In this post, we will be writing a simple Selenium script that compares the page title of the currently opened web page to the title string that we are setting. If both of them match print Pass otherwise Fail.
Steps to be automated:
- Launch the Browser: Chrome, Firefox and Internet Explorer (We will see the examples for all 3 browsers).
- Open the website http://www.seleniumhq.org/
- Get the title of the page.
- Match it with a title that we are providing.
- If both the title match, print Pass otherwise print Fail.
- Close the Browser.
So, we will start by creating a package and a class file. If you’re facing issues with this you can refer this article where I have explained in detail how to create a package and class file. We have created a package com.testersdock.testcase and inside that a class file SeleniumTC.
To start off, we will be writing the selenium script for Chrome Browser. To launch chrome we will be requiring chrome driver. To download Chrome driver (current version is 2.30 at the time of writing this article), go to https://chromedriver.storage.googleapis.com/index.html?path=2.30/ and click on chromedriver_win32.zip.
Next, we will write the selenium code to perform our execution.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | package com.testersdock.testcase; public class SeleniumTC { public static void main(String[] args) { String expectedTitle="Selenium - Web Browser Automation"; WebDriver driver; //Telling Selenium to find Chrome Driver System.setProperty("webdriver.chrome.driver", "C:\\selenium\\chromedriver.exe"); // Initialize browser driver=new ChromeDriver(); //Launch the Selenium HQ Website driver.get("http://www.seleniumhq.org/"); //Get the Title of the Website String actualTitle = driver.getTitle(); //Check COnditions for Pass or Fail if (actualTitle.contentEquals(expectedTitle)){ System.out.println("Pass"); } else { System.out.println("Fail"); } // Close the browser driver.quit(); } } |
Understanding the Code written:
- String expectedTitle=”Selenium – Web Browser Automation”: We will be matching the web page title with this string.
- WebDriver driver, driver=new ChromeDriver() : Instantiation of driver object.
- System.setProperty(“webdriver.chrome.driver”, “C:\\selenium\\chromedriver.exe”): We are telling selenium to find Chrome binary file and launch it.
- driver.get(“http://www.seleniumhq.org/”): Launching a new browser session by passing the website URL as a parameter.
- String actualTitle = driver.getTitle(): Getting the title of the currently loaded web page and saving it in actualTitle variable.
- if (actualTitle.contentEquals(expectedTitle)){ System.out.println(“Pass”); } else { System.out.println(“Fail”); }: Comparing the the two titles, expected and actual; if it matches then we will get Pass in console or else Fail.
- driver.quit(): Closing the browser window and ending the session.
Upon writing the code, the eclipse will show some errors.
To resolve these errors, we need to import certain packages. To import the packages, hover your mouse to the red-underlined texts, in our case its WebDriver and ChromeDriver and eclipse will open a pop up for fix suggestions.
After importing the packages, all the errors will be resolved and our code is ready to be executed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | package com.testersdock.testcase; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class SeleniumTC { public static void main(String[] args) { String expectedTitle="Selenium - Web Browser Automation"; WebDriver driver; //Telling Selenium to find Chrome Driver System.setProperty("webdriver.chrome.driver", "C:\\selenium\\chromedriver.exe"); // Initialize browser driver=new ChromeDriver(); //Launch the Selenium HQ Website driver.get("http://www.seleniumhq.org/"); //Get the Title of the Website String actualTitle = driver.getTitle(); //Check COnditions for Pass or Fail if (actualTitle.contentEquals(expectedTitle)){ System.out.println("Pass"); } else { System.out.println("Fail"); } // Close the browser driver.quit(); } } |
In the above code, you can see two lines have been added starting with import.org. These are the packages that have been imported.
To run the code, Right-click on the Class file and select Run As -> Java Application.
Upon successful execution, you should see Pass in the console.
Similarly For Other Browsers:
Firefox: Before Selenium 3, we didn’t require any additional drivers for Firefox, but post Selenium 3, there is an additional driver called Gecko driver required, to run Selenium. Download the Gecko driver from https://github.com/mozilla/geckodriver/releases. Extract the geckodriver.exe file from the zip file and make the below changes in the above code:
1 2 3 | WebDriver driver; System.setProperty("webdriver.gecko.driver","C:\\selenium\\geckodriver.exe"); driver = new FirefoxDriver(); |
Internet Explorer: For IE also we have to download the IE driver file. Go to http://selenium-release.storage.googleapis.com/index.html and download the IE 32 or 64-bit driver.
Make the below changes to run the selenium script on IE.
1 2 3 | WebDriver driver; System.setProperty("webdriver.ie.driver", "C:\\selenium\\IEDriverServer.exe"); driver = new InternetExplorerDriver(); |