Manual testing has become obsolete these days because it leads to more number of resources per task and increases cost per project. Automating regression and functional suites and tests with the help of Selenium and the WebDriver API is a good choice to reduce manual intervention and the cost associated. Selenium supports multiple programming languages like Java, Python, and Ruby and C #
Selenium has support of some of the largest browser vendors like Google Chrome, Internet Explorer and Safari. Thus, Selenium is considered as one of the most preferred open source tools for automation testing. Now, let us delve into more detail how the website and web apps can be automated using Selenium.
How To Start With Selenium
First, you need to analyze the application you want to automate. Next, you should know whether you want to do it with record and play or writing robust, browser-based regression automation suites and tests.
If you don’t need a full-fledged framework and if the tests which you want to automate are quite simple then you can go for record and play feature of Selenium IDE. It is implemented as Chrome and Firefox Extension. It has intelligent field selection which can identify an element using ID, Class and Xpath.
You can even debug and set breakpoints. You can put all test cases in single project file which contains all test cases and suites. It is very easy to learn as selenese commands are quite simple and the best thing is that you can do it easily without having any programming knowledge.
Selenium RC can also be used for writing automated web application tests. You can write test in any programming language against any HTTP website using any JavaScript enabled browser. It comes in two parts. One is the server which automatically launches the browser and kills them. It also acts as a HTTP Proxy for web request from them.
Second are the client libraries. It can be used for AJAX based web user interfaces and for some new browser which gets launched in market. It is getting deprecated with the advent of selenium Web driver which eliminated the need of the server which acts as an intermediate between Browser and Java Client.
Selenium Web Driver is the most powerful tool of Selenium. It is extended version of Selenium RC. Web driver has given its support to many browsers like Opera, Safari, Chrome and Internet Explorer. Unlike Selenium RC, it don’t need server to be started prior to the execution. Selenium RC + Web driver API is combined known as Selenium 2.0. It can handle dynamic Web Pages and Ajax calls. Web driver makes direct calls to the Browser and the entire test script is executed using browser’s support and capabilities. Its speed of executing test cases against any browser is very fast as compared to Selenium RC and IDE.
With Web Driver you can deal with complex type of web elements like check boxes, drop downs and alerts. It can handle ajax calls and can switch between windows.
Now we will see how we can get started with seven basic steps of automating one application or web app. Before that you should have downloaded selenium jars and add them into your project’s build path.
- Creating WebDriver Instance:
You need to create Webdriver instance. You can make a reference of WebDriver and you can have child object of its implementation classes like GoogleChrome, Internet Explorer or others.
WebDriver driver= new FirefoxDriver();
WebDriver driver = new ChromeDriver();
WebDriver driver = new InternetExplorerDriver();
Also, keep in mind that if you are using ChromeDriver and InternetExplorerDriver then you need to set path for their executable file with the help of System.setproperty.
System.setProperty(“webdriver.chrome.driver”, “Path”);
System.setproperty(“webdriver.ie.driver”,”Path”);
You don’t have to set it for firefix but if your selenium version is above 3.0 then you need to set path of gecko driver in your code.
System.setProperty(“webdriver.gecko.driver”,”Path”);
- Navigate To Webpage Which You Want To Test:
After making instance of webdriver now it is time to actually open the web application which you want to test. You can navigate to webdriver using get or navigate.to.URL() function.
driver. get(“URL”); // This will land you to our webpage which you want to test.
- Locate an HTML Element on Webpage:
After you have landed on the webpage you can now interact with the webpage using HTML elements. There are many locators which you can use to find out the elements. They are name, class name, Xpath, CSS and ID. You can take help of firebug or developer tools to find out your desired element.
WebElement login = driver.findElement(By.id(“”)); // one example of locator
- Perform an Action on WebElement :
When you have found web element, now comes turn to do some action on it. For example if it is username you can enter words from keyboard and if it is login button, you simply have to click on that.
username.sendKeys(“”); // for sending input from keyboard to username textbox
login.click(); // for clicking on login button
- Anticipate The Browser Response to the Action:
Browser takes some time to actually load the page. You should have some default time wait for all the web elements till the life time of driver instance. This can be done by implicit wait in Selenium.
driver.manage().timeouts().implicitly Wait(10,TimeUnit.SECONDS) ;
This time would be applicable for all web elements. But sometimes there is a situation when only one element takes longer time to load then in such cases you can use explicit wait.
WebDriverWait wait=new WebDriverWait(driver, 20);
wait. until(ExpectedConditions.visibilityOfElementLocated(By.xpath(“”)));
You can have N number of conditions with wait object.
- Close Browser Session
You can close the browser the required test is performed.
driver.close(); // will close the current session
- Run Tests and Record Test Results Using a Test Framework
Whatever I have explained till now will help you to run a single test. You can even maintain a framework to run test suites with beautiful report generation. You can take help of Maven, POM and Test NG to make such a robust framework.
In this way, you will be able to run your test against any web application and web apps. Web Driver is very powerful amongst all selenium tools so, go for it. Learn some programming language and dwindle your manual efforts!!