Prerequisites for writing Selenium WebDriver Script:
Scenario’s:
- Launch firefox browser and open “stqatools.com”.
- Wait until full page load.
- Get page title and store into string.
- Store Expected page title into string.
- Verify Actual and Expected page title then print the verification result.
- Close the web browser.
Example: First Selenium WebDriver script:
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 36 37 | import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class First_Script { public static void main(String[] args) { // Set gecko driver location System.setProperty("webdriver.gecko.driver", "D:\\geckodriver\\geckodriver.exe"); // Create object of webdriver WebDriver driver = new FirefoxDriver(); // Open url driver.get("https://stqatools.com"); // Wait until page load driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); // Get page title String Actual = driver.getTitle(); // Get Expected page title String Expected = "Software Testing Quality Automation Tutorials"; // compare the actual title with expected one if (Actual.equals(Expected)) { System.out.println("Test Passed!"); } else { System.out.println("Test Failed"); } driver.close(); } } |
Output :
1 | Test Passed! |
Import packages:
- import org.openqa.selenium.WebDriver:- Contains the WebDriver class needed to instantiate a new browser loaded with a specific driver.
- org.openqa.selenium.firefox.FirefoxDriver :- Contains the FirefoxDriver class needed to instantiate a Firefox-specific driver into the browser instantiated by the WebDriver class.
To instantiate driver object:
- Setting GeckoDriver path on windows for Selenium WebDriver script.
1 2 3 | System.setProperty("webdriver.gecko.driver","D:\\Workspace\\geckodriver\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); |
Launch a browser:
- We will use webdriver’s get() method to launch a new browser session and redirects it to the specified URL.
1 | WebDriver driver = new FirefoxDriver(); |
Wait Until Page load:
- Using implicitlywait wait until page loading
1 | driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); |
Get the actual page title:
- The webdriver class has the getTitle() method that is used to get the page title of the current page.
1 | String Actual = driver.getTitle(); |
Store Expected page title into string:
1 | String Expected = "Software Testing Quality Automation Tutorials"; |
Compare the expected and actual values:
- The following code is use to compare the actual title with the expected one.
1 2 3 4 5 6 7 8 9 10 11 | // compare the actual title with expected one if (Actual.equals(Expected)) { System.out.println("Test Passed!"); } else { System.out.println("Test Failed"); } |
Terminating a browser session:
- The “close()” method is used to close the browser window.
1 | driver.close(); |
Running the test in eclipse:
- On Eclipse’s menu bar, click Run –> Run
OR
- Press Ctrl+F11 to run the entire code.
Locating GUI WebElements:
- Locating WebElements in WebDriver is done by using the “findElement(By.locator())” method. Using different locators for Selenium WebDriver script.