Categories
Programming

Opening Right-click Menu / Context menu with Selenium WebDriver

Selenium WebDriver providesActionBuilder to perform complex user interactions with a web page. It allows chaining multiple actions together to perform a complex action.

You can create a complex action and execute the action (perform) it in the following manner. (Ruby)

driver.action.key_down(:shift).
              click(element).
              click(second_element).
              key_up(:shift).
              drag_and_drop(element, third_element).
              perform

 

The action to open the context menu is context_click() .

driver.action.context_click(webdriver_element).perform

By default, it will click on the middle of the element if you provide one.  Otherwise, it will click where the mouse pointer is at.

If you want to move the pointer to a specific location of the page before clicking, use methodmove_to.

 

If you want to select an item from the context menu use arrow keys and enter then perform the action. You can also further pinpoint the cursor position by using  custom position from your element reference

page.driver.browser.action.move_to(webdriver_element, 5, 10)
 .context_click
  .send_key(:arrow_down)
 .send_keys(:enter)
 .perform

 

Ref: http://www.rubydoc.info/gems/selenium-webdriver/Selenium/WebDriver/ActionBuilder

Categories
Programming

Speed up Selenium web-driver tests by disabling images and CSS

Using Selenium Webdriver, we can manipulate and use a web browser programmatically. This is widely used in website testing as once a programmed script is written to run on a website, we can sit back and relax while the script is testing the desired outcomes of the website. Also there are many other use cases.

Now the thing about the driver is that it uses a real web browser to run the scripts. So once a web page is accessed, the browser will perform its normal functionality loading everything in the web page. This can slow down the script. However for your use case you might not need to wait till images are rendered or CSS files being loaded etc.

So I wanted to disable the images, CSS for my use case when I’m running the script on Firefox. So if you are ok with not having load the images, CSS and disable other stuff, you can manipulate Firefox profiler in the test window to achieve that. I think other browser can do similar like this to archive the same thing.  The code segment below works for Firefox.

## Get profile class
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
## get the Firefox profile object
firefoxProfile = FirefoxProfile()
## Disable CSS
firefoxProfile.set_preference('permissions.default.stylesheet', 2)
## Disable images
firefoxProfile.set_preference('permissions.default.image', 2)
## Disable JavaScript
firefoxProfile.set_preference('javascript.enabled', False)
## Disable Flash
firefoxProfile.set_preference('dom.ipc.plugins.enabled.libflashplayer.so','false')
## Use the driver
browser = webdriver.Firefox(firefoxProfile)

That’s it. Now images, CSS and other stuff will not be loaded.

Conclusion

If disabling images, and other stuff break your website you will not get any value from this approach however for certain cases this might come handy. And it’s a great option to have if you want to speed up the execution of the scripts.