Shifting left with DevOps and Continuous Integration

Adopting Continuous delivery helps to achieve rapid application development throughout the software application life cycle. It is a methodology, a mindset change,a shift-left approach and a leadership practice to streamline manual processes and enforce consistency and repeat-ability in software delivery pipeline. This is about enhancing collaboration and shared matrices and processes across Developers, QA and Ops team.

Read time – 10 minutes

In order to establish a Continuous Delivery environment, the most important requirement is the implementation of an automated Continuous Integration (CI) System. The CI process involves all stages – right from code commits to a version control system (done on the CI server) that serves as a kick off for a build to compile, run tests, and finally package the code. DevOps is playing a major role in going towards defect-preventive approach.

This blog will demonstrate the steps and advantages of implementing a Continuous Integration System using Jenkins and a group of virtual machines. You can utilize your CI system for automatic infrastructure setup and execution of suitable automated scripts whenever a new build or commit happens in that automation script code.

Highlights

  1. Setting up a Jenkins Master machine
  2. Setting Jenkins slaves for distributed execution
  3. Creating new Jenkins job for new scripts
  4. Creating execution pipeline for automating the build steps
  5. Scheduling script execution

 

  • Setting up a Jenkins Master Machine – Installation of Jenkins is very easy; it is just about executing a jar file or executable. Once this is up, it can be accessed from any machine available on that network through a web browser. Jenkins Master is responsible for re-directing all commands and execution to Slave machines. For set up instructions, go through Setting up Jenkins in 5 minutes
  • Setting Jenkins Slave for distributed execution – Jenkins Slave machine can be a real machine, a virtual machine or a dockerized container which has capability of an operating system. Jenkins slave can also be set up by just executing a jar file and registering that machine as a slave against Master Jenkins machine. When Jenkins master receives instructions, it processes that and decides which script would be executed on which slave machine. So if multiple slaves are connected, execution can be done in distributed manner and will result in multi processing + multi threading of execution.
  • Creating new jobs for script execution – A job in Jenkins is about defining a series of action for successful execution of script. First it fetches the latest script code from sub-version, and then it builds that code on some selected slave or master (which is decided by Jenkins Master). Once the code is built on slave machine, Scripts get started and executed. usually, we use Selenium, Java and Maven for automated build process. Once a job is created, it has a web URL. This web URL is helpful in sharing execution results, build info etc.
  • Creating execution pipe line – Execution pipe line is a visual representation of different job executions in sequence. This helps in automating the whole process of script execution. So once all Jenkins jobs are set up, you can decide the order of their execution based on the type of script e.g. Smoke, Regression, release validation, web services test etc. So it creates a pipe line of execution which has a single trigger point. That trigger is initiated whenever a new build / release happen on platform or there is any requirement of executing it. Once a single job in this pipeline gets finished, it automatically triggers the next job in the pipe line. QA/Developer keeps getting the continuous feedback of automated results, which helps us in identifying the defects. Below diagrams represents a pipe line which shows headless scripts execution which is done using plugin – Build pipeline plugin

build-pipeline

  • Scheduling script execution – Jenkins job is that these can be scheduled for future executions and can provide results of nightly test build script executions. It helps a lot of time spent in manual triggering and monitoring of execution and enhance the automated process of script execution. There are two ways to do this-
  1. Jenkins plugin for scheduling – Build schedule plugin
  2. Select the option for build periodically in build steps and define a regular expression which is a 5 digits separated number define the execution timing and cycle. for e.g. below images showing that this job will be executed at 22:00 (10:00 PM) daily.schedule

I have setup this kind of infrastructure for automated test script execution, Next step is to do the same at build deployment level. I will keep posting more stuff related to Devops and continuous integration. Happy Testing 🙂

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

Continuous integration with Jenkins in 5 minutes

When we talk about continuous integration; there are many tools like Jenkins, TeamCity, Bamboo etc. But Jenkins is the most popular because of its numerous plugin and free style configurations for jobs. Jenkins is an open source continues integration tool, which can be used for various purpose as it provides numbers of Plugin. There are two methods for installation and set up of Jenkins-

1) JAVA Web Archive (JAVA War file)

2) Native package

In this blog, I will discuss in details about how you can do Jenkins setup in 5 mins. So be with me and follow below steps-

Jenkins Installation & Set Up-

1) Download the Jenkins.war file from the following URL-http://jenkins-ci.org/

2) JDK 1.5 or above should be installed and a JAVA_HOME variable path should be set in advance

3) Open cmd/bash/shell according to operating system-Windows/Linux/Mac

4)  Locate the folder where Jenkins war file is downloaded

5) Type java -jar jenkins.war and hit Enter

6) Now open browser and hit the URL- localhost:8080

7) If a Jenkins page is opened in browser, that means you have done Jenkins setup

Above steps are for setting up Jenkins. Now Jenkins provide many plugins for various purposes, so you can do other configurations according to your need

Integrating GitHub to Jenkins-

Purpose of this integration is to perform all Github related task using Jenkins. For doing this configuration GitHub should be installed in the system on which Jenkins is deployed.

Please follow following steps for integrating Github-

1) In the Jenkins web GUI, click on Manage Jenkins link and then Click on Manage Plugin

jenkins

managejenkins

2) In the Manage Plugins, click on Available and install following plugins-

  1. a) Github API Plugin
  2. b) Git Client Plugin
  3. c) Git Plugin
  4. d) Github Plugin

 

3) After the installation of above Plugins, Click on Configure System under Manage Jenkins page and set the Git.exe property to its exact path as you can see in screenshot-

git

The Git path shown in the image may vary form system to system configuration.

Note- Above option will be available only if all Git Plugins stated above are correctly installed

4) Save all the configuration settings by clicking on Save button.

Creation of New Job and setting up a Git repository- I will be demonstrating a Github integration for a test project where all the test scripts are on github and Jenkins would be responsible for checking out the scripts and executing scripts on the machine where Jenkins is deployed. Please refer to following steps to do above set up

1) Click on New Job link in the Jenkins Dashboard.

2) Create a free style Software project and click on OK button.

newjob

3) After creating the job click on Configure and select Git under Source Code Management option-

job2.png

Enter a Repository URL as seen in the screenshot, which will be a combination of Username, Password and Repository Name.

Enter a Branch Name if you want to check out the code from a specific branch. If the field left blank, it will refer to Master branch by default.

4)Under Build option select your preferred build method for script execution and write the command accordingly- Please refer to attach screenshot-

build1

build2.png

5) Save the configuration and click on Build Now, if the set up is done correctly your scripts/code starts executing

This is about how you can do quick setup using Jenkins war file. similarly you can do setup using native programs of Jenkins and can create Client-Server modal for same.

Happy Learning 🙂

Multiple Window Handling in Selenium

Today most of the web applications contain multiple navigation links which lead the user to other windows. It is quite possible that at the same time, functionality is performed using two or more windows.  This can be quite a tricky situation although it might sound a simple one. This was something I faced while automating a web application, as working with multiple windows (sometimes even 3-4 windows at the same time) was an essential part of this application).

Selenium WebDriver assigns an alphanumeric id to each window as soon as the WebDriver object is instantiated. This unique alphanumeric id is called window handle. Selenium uses this unique id to switch control among several windows. In simple terms, each unique window has a unique ID, so that Selenium can differentiate when it is switching controls from one window to the other. I will explain it using below example-

public void multipleWindowsHandle() {
        driver.get("http://the-internet.herokuapp.com/windows");
        // Store the first window handle
        String firstWindow = driver.getWindowHandle();
        String newWindow = "";
        driver.findElement(By.cssSelector(".example a")).click();
        //You might need to add wait here to wait for second window to appear
        Set<String> allWindows = driver.getWindowHandles();
        //iterate in window handles and switch to the different window
        for (String window : allWindows) {
            if (!window.equals(firstWindow)) {
                newWindow = window;
            }
        }

        driver.switchTo().window(newWindow);
        if(driver.getTitle().equals("New Window"))){
          System.out.println("You are now on new window");
        }
    }

}

Following is the expected behavior from above code-

  • Visit the page
  • Click to open a new window
  • Switch between the windows
  • Check the page title to make sure the correct window is in focus

First we have stored single window handle and after click operation we are storing all window handle. Once we have all window handle, we are switching to expected window. Important thing to understand here is that method getWindowHandles() returns a Set. A set never store data in sorted manner(its random), so don’t make a mistake of accessing window handles on basis of index.

 

Now when we click on link in above scenario, based on the network speed it might took sometime to open the new window. So following can be some solutions-

  1. Thread.Sleep() could be one solution but we never know the exact time to wait. It also depends on internet speed and processor speed.
  2. Wait till total window handles become two.

Either we can use inbuilt method waitForNumberOfWindowsToBe() of ExpectedConditions class or we can make our own as below-

Code to wait for specific number of windows-

public void waitForNumberOfWindowsToEqual(final int numberOfWindows)               { //Making a new expected condition   
new WebDriverWait(driver,30){}.until(new ExpectedCondition<Boolean>(){   
                   @Override public Boolean apply(WebDriver driver) {

                              return (driver.getWindowHandles().size() == numberOfWindows);}

                          });

}

If you handle this wait in your code, your code become more robust. This is how you can work on multiple windows in Selenium WebDriver.

Happy Learning 🙂

Handling Waits in Selenium WebDriver

While testing the websites, we can’t really quantify the exact response time which an action takes on a web page. So we need to handle the waits between actions on a web page while writing selenium scripts.

So lets see how can we implement a wait strategy which is robust, stable for long run for the scripts. If we think of a Selenium script, it is nothing but a JAVA program (assuming you are implementing it in JAVA); So  what about pausing a java program for some time. So one way is following –

Thread.sleep(number_of_miliseconds)

Thread.sleep(5000L);

But, as I said above we can’t really quantify the time, or we can give maximum time here. But then you will end up spending a lot more time in execution, but purpose of automation is to minimize time. Lets see what are the issues with above strategy-

  1. You can never predict the exact wait time
  2. It increases overhead
  3. It may not work well with all computers

So to handle this, Selenium provides two types of wait strategies-

  1. ImplicitlyWait
  2. WebDriverWait

ImplicitlyWait-

As we know in Selenium every driver.findElement() command looks for the WebElement in the web page. Implicitlywait tells webdriver to wait for an element if they are not immediately  available, So that Webdriver does not throw NoSuchElementException immediately

driver.manage().timeouts().implicitlywait(20,TimeUnit.Seconds);

So the difference between this and sleep strategy is that here maximum threshold is 20 seconds (can be as per your choice). So if element is available in lets say 4 seconds, it wont wait for other 16 seconds. and the beauty is you dont have to write this command again and again, once you write this command, it is set up for complete WebDriver session.

WebDriver Wait or ExplicitWait-

Many times, we need to explicitly wait for some condition to finish and then to proceed the execution. So this time its not only about waiting for an element to come in DOM. so here is what you have to do for it-

WebDriverWait wait=new WebDriverWait(driver, 30);
//Now use this object to apply wait with conditions
wait.until(ExpectedConditions.textToBePresentInElement(driver.findElement(By.xpath(“”)), “expected text”));

So here, webdriver will poll every 500 seconds to check text in given element and maximum threshold here is 30 seconds. WebDriverWait class provides almost every expected conditions which you can come across while creating script. lets have a look by below snapshot-

wait

Some times we come across situations where the provided expected conditions are not enough and we have to create our own condition. That can be done in following manner-

WebDriverWait wait=new WebDriverWait(driver, 30);
//Now use this object to apply wait with conditions
wait.until(new ExpectedCondition<Boolean>(){
@Override
public boolean apply(WebDriver driver){
return boolean_return_type_of_your_condition;
}
});

So every expected condition has an Apply method, which you need to override with your custom condition.

So this is how you can handle waits with your Selenium WebDriver scripts. Hope it has helped you understanding and implementing it. 🙂

Xpath : a path to your WebElement locators

Many times I hear from people that my Selenium script is not executing properly for different browsers. So we keeps on complaining that selenium is not really identifying our web elements on different browsers. But if we look at it deeply, it’s not selenium; it’s the locator engine of your browser which is not able to understand your WebElement’s locator.

So lets talk about the browser’s locator engine, Every browser has different mechanism to process your locator i.e Xpaths, Css Selectors, Ids etc. So when we try to execute our same script on different browser, it might not identify the same element it was  identifying on previous browser. So we need to create locator which has basic requirement to be robust for almost all browsers-

  • Relative xpath over absolute xpath.
  • Avoid using xpath or css selector if element itself has some unique attribute i.e. id, name, linkText, title.
  • Verify your xpath before executing script (Saves a lot of time).
  • Prefer using Css Selector over xpath if you creating your script for headless browsers like PhantomJs.
  • Create your customized locator if needed.

Lets understand the syntax of an xpath-

When you have a unique attribute for your targeted locator

//tagname[@attributename='attribute value']  e.g. //input[@id='username']
when you don't aware of specific tag name
//*[@attributename=’attribute value’] 

When you have no unique attribute but a parent of that element has some unique attribute

//parentTagName[@parentattributename=’attribute value’]/targetedElementTagName
e.g. //table[@id=’userTable’]/div
use double slash "//" if you are targeting to a grand child

Some  Xpath methods-

  1. Contains() – When you want to select an element which contains some part of its attribute, for e.g. you wan to select an element which contains text “user” in its id-
    //*[contains(@id,’user’)]
  2. starts-with() – When you want to select an element whose attribute’s value starts with some text. For e.g. you wan to select an element whose ids starts with text “user”-
    //*[starts-with(@id,’user’)]

Handling text attribute – Sometimes we use Xpath methods with text attribute and it does not work. we need to understand that text is not an element’s attribute, its a function. So if you are applying above conditions for text then it would look like as below-

//*[contains(text(),’user’)]
//*[starts-with(text(),’user’)]

Handling parent-child nodes

  1. accessing immediate parent – //tagname[@attribute=’attributevalue’]/parent::parenttag

e.g. //div[@id=’someid’]/parent::span – (here we are accessing parent span of some div child)

2. accessing grand parent – //tagname[@attribute=’attributevalue’]//ancestor::grandparenttag.

if there are multiple grandparent with same tag you can give index like below- //tagname[@attribute=’attributevalue’]//ancestor::grandparenttag[1]

3. accessing nth-sibling (following and preceding) – Suppose we have a div element and its six child of tag span. We have id of first span but want to access 5th span based on first-

//span[@id=’first_span_id’]/following-sibling::span[5]

for preceding sibling – Suppose we have a div element and its six child of tag span. We have id of 6th span but want to access 1st span based on 6th

//span[@id=’sixth_span_id’]/preceding::span[5]

So this was just little information about xpaths, creating customized xpath etc. I will be posting another blog with detailed information about creating  Css Selectors. Feel free to post comments in case of any concerns.

Happy Coding 🙂