Sunday, July 15, 2018

Getting to know about Selenium WebDriver APIs

Before writing the first case, we should know about Selenium WebDriver APIs. The most commonly used APIs are WebDriver, WebElement, Select, Navigation, Options, Window, Timeouts.

Here is a diagram to give you a general idea.
WebElement and Select.
You can find detail information for those interfaces through selenium java docs here, and documentation here.

We will create a simple test case to show you how to operate different web elements, such as text box, radio button, checkbox, select, multiple select, button, and table.

Before looking into the code, take few minutes to look at the demo site here.

package me.simplejavautomation;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;

public class WebDriverApiTest {

    private WebDriver driver;
    private String url;

    @Before
    public void setUp() {
        driver = new ChromeDriver();
        url = "http://simplejavautomation.blogspot.com/2018/07/demo-page-for-selenium-webdriver-apis.html";
    }

    @After
    public void tearDown() {
        driver.quit();
    }

    @Test
    public void testWebDriverApis() {
        driver.get(url);
        String expectedTitle = "Simple Java Automation: Demo Page for Selenium WebDriver APIs";
        String actualTitle = driver.getTitle();
        assertEquals(expectedTitle, actualTitle);

        String actualUrl = driver.getCurrentUrl();
        assertEquals(url, actualUrl);

        String pageSource = driver.getPageSource();
        assertTrue(pageSource.length() > 0);

        driver.findElement(By.linkText("Simple Java Automation")).click();

        driver.navigate().back();
        driver.navigate().forward();

        String expectedUrl = "http://simplejavautomation.blogspot.com/";
        actualUrl = driver.getCurrentUrl();
        assertEquals(expectedUrl, actualUrl);

        driver.navigate().to(url);
        driver.navigate().refresh();
        actualUrl = driver.getCurrentUrl();
        assertEquals(url, actualUrl);
    }

    @Test
    public void testWebElementApis() {
        // go to the demo page
        driver.get(url);
        // maximize the browser window
        driver.manage().window().maximize();

        // get FirstName, LastName, and Email input and send keys to them
        driver.findElement(By.name("firstname")).sendKeys("Tristan");
        driver.findElement(By.name("lastname")).sendKeys("Zhou");
        driver.findElement(By.name("email")).sendKeys("jiahuan.zhou@yahoo.com");

        // set Male radio button checked
        List sexRadios = driver.findElements(By.name("sex"));
        boolean isMaleSelected = sexRadios.get(0).isSelected();
        if (!isMaleSelected) {
            sexRadios.get(0).click();
        }

        // set experience to 5+
        WebElement expRadio = driver.findElement(By.id("exp-6"));
        expRadio.click();

        // set Automation Tester checked
        List professionChecks = driver.findElements(By.name("profession"));
        for (int i = 0; i < professionChecks.size(); i++) {
            String checkValue = professionChecks.get(i).getAttribute("value");
            if (checkValue.equalsIgnoreCase("Automation Tester")) {
                professionChecks.get(i).click();
                break;
            }
        }

        // check all Automation Tools
        driver.findElement(By.id("tool-0")).click();
        driver.findElement(By.xpath("//*[@value='Selenium IDE']")).click();
        driver.findElement(By.cssSelector("input[value='Selenium Webdriver']")).click();

        // choose Australia for Continents select box
        Select continentSelect = new Select(driver.findElement(By.id("continents")));
        continentSelect.selectByIndex(2);
        driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
        continentSelect.selectByVisibleText("Australia");

        // select Browser and Navigation option
        Select commandSelect = new Select(driver.findElement(By.name("selenium_commands")));
        commandSelect.selectByIndex(0);
        driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
        commandSelect.selectByVisibleText("Navigation");

        // click submit button
        driver.findElement(By.id("submit")).click();

        // validate all inputs which displays in the table
        assertEquals("Tristan", getCellValue(2));
        assertEquals("Zhou", getCellValue(3));
        assertEquals("jiahuan.zhou@yahoo.com", getCellValue(4));
        assertEquals("Male", getCellValue(5));
        assertEquals("5+", getCellValue(6));
        assertEquals("Automation Tester", getCellValue(7));
        assertEquals("QTP, Selenium IDE, Selenium Webdriver", getCellValue(8));
        assertEquals("Australia", getCellValue(9));
        assertEquals("Browser, Navigation", getCellValue(10));

        // move to the table element
        new Actions(driver).moveToElement(driver.findElement(By.id("summary"))).build().perform();

        // sleep 5 seconds
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * Get 2nd cell value for the row.
     * 
     * @param row
     * @return
     */
    private String getCellValue(int row) {
        return driver.findElement(By.xpath("//*[@id='summary']/tbody/tr[" + row + "]/td[2]")).getText();
    }

}

2 comments: