Run selenium test in headless mode with real Chrome and Firefox

Time has always been a factor to measure the efficiency and effectiveness of our automated test scripts. Being CD/CI a crucial need, its very important to run our test quickly. Best way of it is obviously running your test with headless browsers (e.g. GhostDriver, PhantomJs driver), but we have always seen below issues with these browsers-

  1. Same locators(xpaths, CSS selectors) do not work when test executed on headless browsers
  2. Additional line of code to handle cookies and other issues.
  3. Javascript alerts create problems

And there are many more problems apart from above mentioned. These problems happen as Xpath engine and java-script engine implementation varies from browser to browser and specifically for headless browsers. But what if we run our real intended browsers in headless mode during our test execution. That will obviously solve our above problem.

Selenium always surprises us with some cool new features. This time it has just blown the need for headless browsers like phantomjs etc. Now with selenium version 3.6.0 on-wards you can run your real browsers(chrome and Firefox) test in headless mode.

Now lets look at how to make our browser run in headless mode during our test execution. Please make sure to utilize this feature, you are using selenium version 3.6.0 and above. Lets go through a sample code for google test for Firefox and Chrome respectively.

System.setProperty("webdriver.chrome.driver","Path to chrome driver exe");
ChromeOptions options = new ChromeOptions();
options.setHeadless(true); //this line is actually enables the headless mode
WebDriver driver = new ChromeDriver(options);
driver.navigate().to("https://google.com");
driver.findElement(By.name("q")).sendKeys("hello");
driver.close();

Similarly, we can use FirefoxOptions to enable headless mode for Firefox browser

System.setProperty("webdriver.gecko.driver","Path to gecko driver exe");
FirefoxOptions options = new FirefoxOptions();
options.setHeadless(true); //this line is actually enables the headless mode
WebDriver driver = new FirefoxDriver(options);
driver.navigate().to("https://google.com");
driver.findElement(By.name("q")).sendKeys("hello");
driver.close();

Also, its very easy to do your selenium Grid configuration for your remote test execution. please refer to below code snippet.

FirefoxOptions ffoptions = new FirefoxOptions();
ffoptions.setHeadless(true);
RemoteWebDriver driver = new RemoteWebDriver(
        new URL("http://localhost:4444/wd/hub"),
        ffoptions);

I have also done some time comparison with chrome browser headless and non headless mode. Below is time analysis for a simple test which navigate to google and search for some text :

  • Execution time in non-headless mode – 12.193 seconds
  • Execution time in headless mode – 9.321 seconds

This can tremendously reduce execution time when you will execute your large test suites.

Now, its time to say goodbye to your third party headless browsers. Execute the test with real browsers in headless mode.

Cheers, Happy Automating 🙂

Advertisements

Selenium 3: Firefox with Gecko Driver

Simon Stewart had announced Selenium 3 release on 25th May 2013 and it has finally beta released to use on 2nd August 2016. In this blog post I will be discussing the changes happened on implementation level when you will actually be writing code using Selenium 3.0

If you are still not aware with API level changes, then I suggest you to go through my last blog Way to Selenium 3.0 and then get back to this one 🙂

Following are some implementation level changes i have observed after writing Selenium 3.0 first program

  • You need JAVA 8+ to run Selenium 3 test
  • You need Gecko driver (like  chrome and IE driver) to run scripts on Mozilla Firefox.
  • Jar library size is now minimized to 10 MB
  • leg-rc jar is no more bundled in main selenium jar, you need to separately download it.
  • Official support for IE requires version 9 and above
  • Detailed changes in Selenium 3.0.0 can be found at link – Selenium 3 change log

So now when you run your current script with Selenium 3.0 jar files on Firefox browser,you might see below error

java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases at com.google.common.base.Preconditions.checkState(Preconditions.java:199) at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:109) at org.openqa.selenium.firefox.GeckoDriverService.access$100(GeckoDriverService.java:38) at org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:91) at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:296) at org.openqa.selenium.firefox.FirefoxDriver.createCommandExecutor(FirefoxDriver.java:245) at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:220) at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:215) at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:211) at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:124)

So now you need Gecko driver to execute scripts on Firefox, Gecko driver exe can be downloaded from – GeckoDriver. Now you need to specify the system property with gecko path

System.setProperty("webdriver.gecko.driver","path of geckodriver.exe");
WebDriver driver = new FirefoxDriver();

What is Geckodriver – A Proxy for using W3C WebDriver-compatible clients to interact with Gecko-based browsers. Geckodriver provides HTTP API described by the WebDriver protocol to communicate with Gecko browsers, such as Firefox (Version after 47). Even if you are working with older versions of Firefox browser, Selenium 3 expects you to set path to the driver executable by the webdriver.gecko.driver.

P.S. If you have automated build system dependent on Selenium scripts(critical cases), I recommend you to wait for official non beta release for selenium and then update the libraries of your build system. Its always better to use stable version of any software.

Happy Coding 🙂

 

How to choose your IDE for development or test automation

As an automation tester, we spend a lot of time playing around the code, debugging it and reusing it. It’s important to have a good IDE (integrated development environment) where you can save a lot of time while writing code for your automation script.

Although the criteria for selecting the IDE is common for every language. It also depends on the personal preferences, and the kind of requirement one have in their project. In this blog post , I will talk about how to choose your JAVA IDE for Selenium WebDriver test.  An IDE should have following capabilities

  • Little configuration
  • Flexible
  • In-built plugin and build deployment support
  • Smart enough in code completion (most important)
  • Powerful, smart debugging

I have recently started working on a new selenium java project.For me it was not that hard to choose as I had most of experience using Eclipse from my previous projects and no experience using Netbeans and IntelliJ at all. This time I have thought of doing  experiment by using some other IDE

I was a big fan of Eclipse until I figure out the capabilities of IntelliJ IDEA. I have used IntelliJ IDEA community edition and following are some awesome features I have identified which I didn’t notice in Eclipse-

  1. It provides auto-suggestions while reusing variables, keywords and out of box method highlighting
  2.  If you have used some code for which reference is not added, it will suggest you to find maven info and add that in to pom.xml
  3. There is no need to create work space, you can start using/building any project from anywhere
  4. It has in-built support for identifying the possible plugin which can easy user’s work and will suggest you to install that (e.g. if you are using feature file in code , it will suggest to use cucumber plugin automatically)
  5. It has inbuilt ANT, Maven and Gradle support, while creating a java project in IntelliJ it automatically create Gradle, Build.xml and Pom.xml files
  6. It provides inbuilt Git, SVN, TFS support without installing any additional plugin

In a nutshell, conclusion is –

  • Eclispe – Very flexible, not very smart in code completion
  • IntelliJ IDEA – flexible, powerful, best code completion, smart,user-friendly
  • NetBeans – user-friendly, good for JAVA Enterprise beans projects

Current Market trend

According to a recent report by SoftSys the most used IDE by Java developers is Eclipse, which is open source and free to use. The next one is IntelliJ followed by Netbeans. Thus, more often than not cost can be one of the factors, specially if there is not much difference in features between different IDEs. From the perspective of a beginner almost all the IDEs seem identical in terms of their feature set, however, I am sure there are many differences in the details of how those features are exposed and how convenient it is to use them. So, is it just a matter of preference that some people like Eclipse and others like IntelliJ-

java-web-and-application-development-services-8-638

According to a recent survey, when people directly asked about choosing the IDE, here is what there response was –

1a5d510

So it’s very clear that as Agile is the new SDLC where all part of software development are aggressively done, so its better to have a powerful and smart IDE for development and save our time. 🙂

How did you choose your current IDE? What are the features that matter when deciding on an IDE? How much would you be willing to pay for it? Do let me know of your thoughts in the comments discussion.

Happy coding 🙂

 

Selenium WebDriver : An ecosystem of browser standards

Yes!.. You read that right, Selenium WebDriver : An ecosystem of browser standards. We says that WebDriver is a tool which automates browser. But looking at its W3C standard being implemented by almost every browser vendor, It seems to me a Creator of  an ecosystem for browser standards. 

Just have a look at the evolution happening around WebDriver –

  1. Selenium 3.0 is soon going to be released with most of the W3C standard
  2. Apple has announced that Safari 10.0 would have official WebDriver support
  3. Microsoft now has official support for WebDriver with Edge browser
  4. Mozilla has released Marionette(an automation driver for Mozilla’s Gecko engine)
  5. WebDriver is not able to launch FF47.0 (but that’s not an issue with WebDriver, that is a Firefox issue 😀 )

All above facts clearly stats that all the browser vendors are focusing so much to implement W3C standard , so that doing cross-browser automation testing is no more a hectic task.

Now lets spot some light on all the points discussed above.

  • Selenium 3.0 – The details about selenium 3 can be found in my previous blog post – Way to Selenium 3.0
  • Apple’s Safari 10.0 release –  WebDriver is mentioned as one of the feature of Safari 10.0 on its official release page –Apple’s Safari 10.0. It says that  – Safari on OS X supports WebDriver, which lets you automate web-content testing. It provides a set of interfaces to manipulate DOM elements and control the browser’s behavior. You can enable Remote Automation in the Develop menu and then launch the server using /usr/bin/safaridriver
  • Microsoft’s Edge release – “The Microsoft Edge implementation of WebDriver supports both the W3C WebDriver specification and the JSON Wire Protocol for backwards compatibility with existing tests”. More details can be found at –Microsoft Edge WebDriver guide
  • Marionette driver by Mozilla – Marionette is an automation driver for Mozilla’s Gecko engine. It can remotely control either the UI or the internal JavaScript of a Gecko platform, such as Firefox. It can control both the chrome (i.e. menus and functions) or the content (the webpage loaded inside the browsing context), giving a high level of control and ability to replicate user actions. In addition to performing actions on the browser, Marionette can also read the properties and attributes of the DOM.
  • Recently one of my colleague had upgraded Firefox 46.0 to 47.0 and existing WebDriver scripts was breaking with error “Firefox 47 – Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms“. After a lot of debugging and going through the link – Issue With FF47.0, I got to know that this is some issue with FF 47.0 and fixed in FF 48.0 which is not yet released. I just felt proud that an automation tool is identifying issues in a Browser. 😀

WebDriver has actually created an ecosystem for all browser. And when this thing would be implemented in all browsers, cross browser testing would be just about changing the browser driver.

Looking forward to see all the browsers implementing this W3C standard and making our cross browser automation work so transparent, error free and simple 🙂

Way to Selenium 3.0

 

Selenium 3 is on its way to be shipped. Simon Stewart (Selenium Project Lead) along with Applitools has given a sneak peek about Selenium3.0 on 25th May 2016. The Webinar was just great as all other presentations by Simon Stewart. I have identified few points and will try to summaries in this blog post-

Main aim for Selenium 3 is to be – “a tool for user-focused automation of mobile and web apps“. this one liner explains it all about Selenium 3 . So they are going to focus on automation of web and mobile apps only (No support for any other kind of application). For Selenium 3, They have primarily worked on making the technology behind Selenium as stable and capable as possible.

For understanding the changes happening in Selenium3, you really need to understand how selenium has been evolved since the time Selenium-Core was released. During this blog post, I will be going  through following points-

  1. The past of Selenium project – how it evolved
  2. Where is Selenium 3.0
  3. User visible changes
  4. Back-end changes

Evolution of Selenium  – Json huggins from throughworks has created a javascript test runner which was capable of injecting Javascript in html pages and perform the required action. User need to create their script in html which is not really a programming language. Due to JavaScript’s same origin policy, user was not able to perform action on the different domain (websites). Then RC came in picture, where your script can actually communicate to a proxy server(RC server) and trick your browser to believe that all the requests are coming from same domain. It was really hard to maintain the Selenium RC/Core tool for the Selenium developer, so they have started working on creating a standard api which can perform all action required on a web page irrespective of the language in which scripts are written.

Simon Stewart then discovered WebDriver api which works on JSON wire protocol over http, and don’t have any language specific dependency. Then this api was wrapped in to language specific bindings to support multiple languages and this was called Selenium 2.0

Where is Selenium 3 – There are two level changes; back-end level changes and user visible changes.

User visible changes

  1. Selenium Core would completely be removed.
  2. Selenium RC would be deprecated, there would be no active development to support Core or RC except the very urgent fixes.
  3. The RC api would be backed by WebDriver, so you can execute your existing RC script (WebDriver is recommended for all active development for new scritps)
  4. Client bindings for other languages except for JAVA are still same
  5. Using WebDriver after quit() would be now an illegal State Exception
  6. For mobile users, the Selenium 3 will be hosting a suite of tests to facilitate interoperability between the many different projects available (i.e. Appium, ios-driver, Selendroid etc.) that are extending the WebDriver API to also cope with mobile.
  7. New Java binding structure for Selenium3 is as follows-
  • Selenium – JAVA : just the webdriver classes
  • Selenium 3 server : Lighweight, command line compatible remote server
  • leg-rc : the old selenium client side classes, WebDriverBackedSelenium, Remote end point for Se3 server

As there would be no support for RC and core, existing scripts would be executed by leg-rc’s “WebDriverBackedSelenium”. Selenium RC server would be replaced by Selenium 3 server which would be command line compatible that means you can access its features through command line. it would really help in continuous integration. Selenium3 server might also have capability to hook in to already opened browser windows.

Backend level changes – Due to support for many browsers by Selenium 2.0 , it was really hard for developer to fix selenium specific issues. For example fixing an issue for one browser (chrome) trigger some other issue for different browser(Firefox). The reason for this inconsistency is that every browser follows different approaches for Xpath engines and other browser mechanism. So Selenium has come up with W3C standard and asked every browser owner to follow these standard while implementing browser. Selenium developer community has also recommended every browser vendor to own the drivers as they are more familiar with their browser. So apart from these following can be changes from back-end side-

  1. Migrate all drivers to use the status strings rather than status codes in responses
  2. All actions to have a single end point for JSON wire protocol service
  3. Migration to netty or webitt server

There is no changes in terms of additional features. They are targeting to have new features specific to mobile in Selenium 4.0. Selenium 5.0 would be completely W3C compatible. Selenium3 and Selenium4 would have interoperability i.e. client bindings of Selenium3 can communicate to Selenium 4 server and vice – versa.

If we look in terms of stability and back-end changes for Selenium , it is a big change. That is why they have changed a major version from 2.0 to 3.0

The old versions will still be available as a separate download, but active development will cease, except for very urgent fixes.

Hopefully , it would be available to download on official Selenium site. I hope i have covered all the features and points related to Selenium 3, feel free to add in comments if anything is missed. 🙂

EDIT 1 : For Q&A session with Simon Stewart during this webinar, please refer to comments of this blog post. 🙂

EDIT 2 : Selenium 3 beta is now officially released, please visit my latest blog – Selenium 3: Firefox with Gecko Driver