Example usage for org.openqa.selenium WebDriver findElement

List of usage examples for org.openqa.selenium WebDriver findElement

Introduction

In this page you can find the example usage for org.openqa.selenium WebDriver findElement.

Prototype

@Override
WebElement findElement(By by);

Source Link

Document

Find the first WebElement using the given method.

Usage

From source file:FindByLinkText.java

public static void main(String[] args) throws Exception {
    String baseURL = "http://demostore.x-cart.com/";
    System.setProperty("webdriver.gecko.driver",
            "/Users/srijana/Documents/seleniumpractise notes/geckodriver 2");
    WebDriver driver = new FirefoxDriver();

    driver.get(baseURL);/*from   w w  w .  ja va  2  s.  co  m*/

    //driver.findElement(By.xpath(".//*[@id='header-area']/div[1]/div/div[4]/div/ul/li[4]/a/span")).click();

    //driver.findElement(By.xpath(".//*[@id='header-area']/div[1]/div/div[4]/div/ul/li[7]/a/span")).click();

    driver.findElement(By.tagName("a")).click();

}

From source file:IdXPATHDemo.java

public static void main(String[] args) {
    String baseURL = "http://www.google.com";
    System.setProperty("webdriver.gecko.driver",
            "/Users/srijana/Documents/seleniumpractise notes/geckodriver 2");
    WebDriver driver = new FirefoxDriver();

    driver.get(baseURL);//from ww  w.ja  v a2s . c  o  m

    driver.findElement(By.id("lst-ib")).sendKeys("letskodeit");
    driver.findElement(By.xpath("//*[@id='sblsbb']/button")).click();
}

From source file:SmokeTester_UnitTest.java

public void executeSmokeTest() throws Exception {
    // Create a new instance of the Firefox driver
    // Notice that the remainder of the code relies on the interface, 
    // not the implementation.

    System.setProperty("webdriver.chrome.driver", "C:\\Nino\\ChromeWebDriver\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();

    TestCaseReader tcreader = new TestCaseReader();
    List<TestScriptTemplate> tcl = tcreader.readExcel();

    List<TestScriptTemplate> validatedTestScript = new ArrayList();

    String log_execution = "";
    Iterator<TestScriptTemplate> i = tcl.iterator();
    while (i.hasNext()) {
        TestScriptTemplate testscript = i.next();
        //collect the results
        TestScriptTemplate testexecution = new TestScriptTemplate();

        testexecution.setAppCode(testscript.getAppCode());
        log_execution = log_execution + "\nStart smoke testing for application code: "
                + testexecution.getAppCode();

        //access the URL
        driver.get(testscript.getAppURL());

        //login if not yet
        if (driver.getCurrentUrl().contains("identity.safeway.com")) {
            //key in userid and password
            WebElement weusername = driver.findElement(By.id("username"));
            //System.out.println("tag:" + weusername.getTagName());
            weusername.sendKeys(testscript.getAppUserID());

            WebElement wepassword = driver.findElement(By.id("password"));
            //System.out.println("tag:" + wepassword.getTagName());
            wepassword.sendKeys(testscript.getAppPassword());

            WebElement weloginform = driver.findElement(By.name("loginData"));
            //System.out.println("tag:" + weloginform.getTagName());
            weloginform.submit();/*  ww  w.  ja v  a 2s .com*/
            log_execution = log_execution + " Login Successful";
        }

        //recoding URL; required so no need to check for nullity
        testexecution.setAppURL(driver.getCurrentUrl());
        log_execution = log_execution + " Current URL: " + driver.getCurrentUrl();
        //recoding title; required so no need to check for nullity
        testexecution.setHomePageTitle(driver.getTitle());
        log_execution = log_execution + " Page Title: " + driver.getTitle();

        if (isElementExist(testscript.getHomePageElementType(), testscript.getHomePageElement(), driver)) {
            System.out.println("Element match!" + testscript.getHomePageElement());
            log_execution = log_execution + " Home Page Element validation...";
            testexecution.setHomePageElement(testscript.getHomePageElement());
        } else {
            testexecution.setHomePageElement("not found");
        }

        //next page validation
        if (!testscript.getLevel1URL().isEmpty() || !testscript.getLevel1URL().equals("")) {
            //go to next level page
            driver.get(testscript.getLevel1URL());
            log_execution = log_execution + " Next Page validation URL: " + testscript.getLevel1URL();

            testexecution.setLevel1URL(driver.getCurrentUrl());
            System.out.println("execution log: current level 1 URL on set" + testexecution.getLevel1URL());

            if (!testscript.getLevel1PageTitle().isEmpty() || !testscript.getLevel1PageTitle().equals("")) {
                testexecution.setLevel1PageTitle(driver.getTitle());
                log_execution = log_execution + " Next Page title validation: " + driver.getTitle();
            }

            if (isElementExist(testscript.getLevel1ElementType(), testscript.getLevel1Element(), driver)) {
                testexecution.setLevel1Element(testscript.getLevel1Element());
                log_execution = log_execution + " Next Page element validation: "
                        + testscript.getLevel1Element();
            } else {
                testexecution.setLevel1Element("not found");
            }

        }
        testexecution.setLogs(log_execution);
        SmokeTestValidator_UnitTest testvalidator = new SmokeTestValidator_UnitTest(testscript);
        TestScriptTemplate testingresult = testvalidator.getTestResult(testexecution);
        validatedTestScript.add(testingresult);

    }

    tcreader.writetoExcel(validatedTestScript);
    //Close the browser
    driver.quit();
    //return log_execution;
}

From source file:SmokeTester_UnitTest.java

private boolean isElementExist(String elementyype, String elementtosearch, WebDriver driver) {
    System.out.println("input for isElementExist: " + elementyype + "-" + elementtosearch);
    if (elementyype.compareToIgnoreCase("class") == 0) {
        try {/*from   w  w  w .jav  a 2  s. c  o m*/
            driver.findElement(By.className(elementtosearch));
            return true;
        } catch (org.openqa.selenium.NoSuchElementException nse) {
            return false;
        }
    }
    if (elementyype.compareToIgnoreCase("xpath") == 0) {
        try {
            driver.findElement(By.xpath(elementtosearch));
            return true;
        } catch (org.openqa.selenium.NoSuchElementException nse) {
            return false;
        }
    }

    if (elementyype.compareToIgnoreCase("id") == 0) {
        try {
            driver.findElement(By.id(elementtosearch));
            return true;
        } catch (org.openqa.selenium.NoSuchElementException nse) {
            return false;
        }
    }

    return true;
}

From source file:IdNameDemo.java

public static void main(String[] args) {
    String baseURL = "http://www.google.com";
    System.setProperty("webdriver.gecko.driver",
            "/Users/srijana/Documents/seleniumpractise notes/geckodriver 2");
    WebDriver driver = new FirefoxDriver();

    driver.get(baseURL);/*from  w w  w . j av  a  2s.  c  o  m*/

    driver.findElement(By.id("lst-ib")).sendKeys("letskodeit");
    driver.findElement(By.name("btnG")).click();
}

From source file:TestWithSelenium.java

@Test
public void tDomConstructedTableReady() {
    //searching for tbody
    WebElement table = (new WebDriverWait(driver, MAX)).until((ExpectedCondition<WebElement>) (WebDriver d) -> {
        return d.findElement(By.tagName("tbody"));
    });/*from w  ww .  j  a va 2  s  .  c  o  m*/

    Assert.assertThat(table.findElements(By.tagName("tr")).size(), is(5));
}

From source file:TestWithSelenium.java

@Test
public void uTest2002Filter() {
    //searching for filter & sending key
    WebElement filter = (new WebDriverWait(driver, MAX))
            .until((ExpectedCondition<WebElement>) (WebDriver d) -> {
                return d.findElement(By.id("filter"));
            });/*ww w .j  a  va2s  .c  o  m*/
    filter.sendKeys("2002");

    //searching for tbody
    WebElement table = (new WebDriverWait(driver, MAX)).until((ExpectedCondition<WebElement>) (WebDriver d) -> {
        return d.findElement(By.tagName("tbody"));
    });

    //checking whether number of rows is 2
    Assert.assertThat(table.findElements(By.tagName("tr")).size(), is(2));

}

From source file:TestWithSelenium.java

@Test
public void vTestClearFilter() {
    //searching for filter & sending key
    WebElement filter = (new WebDriverWait(driver, MAX))
            .until((ExpectedCondition<WebElement>) (WebDriver d) -> {
                return d.findElement(By.id("filter"));
            });// ww  w .  jav  a  2 s.com

    filter.sendKeys(Keys.BACK_SPACE);

    //searching for tbody
    WebElement table = (new WebDriverWait(driver, MAX)).until((ExpectedCondition<WebElement>) (WebDriver d) -> {
        return d.findElement(By.tagName("tbody"));
    });

    //checking whether number of rows is 5
    Assert.assertThat(table.findElements(By.tagName("tr")).size(), is(5));
}

From source file:TestWithSelenium.java

@Test
public void wTestSorting() {
    //searching for sort button
    WebElement sortButton = (new WebDriverWait(driver, MAX))
            .until((ExpectedCondition<WebElement>) (WebDriver d) -> {
                return d.findElement(By.id("h_year"));
            });/*from  ww w.j  a v  a2 s .  c  o m*/
    sortButton.click();

    //searching for tbody
    WebElement table = (new WebDriverWait(driver, MAX)).until((ExpectedCondition<WebElement>) (WebDriver d) -> {
        return d.findElement(By.tagName("tbody"));
    });

    //listing all tr
    List<WebElement> tableSorted = table.findElements(By.tagName("tr"));
    Assert.assertThat(tableSorted.get(0).findElements(By.tagName("td")).get(0).getText(), is("938"));
    Assert.assertThat(tableSorted.get(4).findElements(By.tagName("td")).get(0).getText(), is("940"));

}

From source file:TestWithSelenium.java

@Test
public void xTestEdit() {
    //searching for tbody
    List<WebElement> tableRows = (new WebDriverWait(driver, MAX))
            .until((ExpectedCondition<WebElement>) (WebDriver d) -> {
                return d.findElement(By.tagName("tbody"));
            }).findElements(By.tagName("tr"));

    WebElement editButton = null;/*from  ww  w . j  a v a  2  s .c  o  m*/
    for (int i = 0; i < tableRows.size(); i++) {
        if (tableRows.get(i).findElements(By.tagName("td")).get(0).getText().equalsIgnoreCase("938")) {
            editButton = tableRows.get(i);
            break;
        }
    }

    editButton.findElements(By.tagName("td")).get(7).findElements(By.tagName("a")).get(0).click();
    WebElement element = driver.findElement(By.id("description"));
    element.clear();
    element.sendKeys("Cool car");
    driver.findElement(By.id("save")).click();

    //searching for new tbody
    List<WebElement> tableRowsUpdated = (new WebDriverWait(driver, MAX))
            .until((ExpectedCondition<WebElement>) (WebDriver d) -> {
                return d.findElement(By.tagName("tbody"));
            }).findElements(By.tagName("tr"));

    String descriptionNew = "";
    for (int i = 0; i < tableRowsUpdated.size(); i++) {
        if (tableRowsUpdated.get(i).findElements(By.tagName("td")).get(0).getText().equalsIgnoreCase("938")) {
            descriptionNew = tableRowsUpdated.get(i).findElements(By.tagName("td")).get(5).getText();
            break;
        }
    }
    assertThat(descriptionNew, is("Cool car"));
}