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:TestWithSelenium.java

@Test
public void yTestError() {
    driver.findElement(By.id("new")).click();
    driver.findElement(By.id("save")).click();

    String submiterrMessage = (new WebDriverWait(driver, MAX))
            .until((ExpectedCondition<String>) (WebDriver d) -> {
                return d.findElement(By.id("submiterr")).getText();
            });/*from  w  ww  . j a v a  2 s  .  com*/

    assertThat(submiterrMessage, is("All fields are required"));

}

From source file:TestWithSelenium.java

@Test
public void zAddNew() {
    driver.findElement(By.id("new")).click();
    driver.findElement(By.id("year")).sendKeys("2008");
    driver.findElement(By.id("registered")).sendKeys("2002-5-5");
    driver.findElement(By.id("make")).sendKeys("Kia");
    driver.findElement(By.id("model")).sendKeys("Rio");
    driver.findElement(By.id("description")).sendKeys("As new");
    driver.findElement(By.id("price")).sendKeys("31000");
    driver.findElement(By.id("save")).click();

    WebElement table = (new WebDriverWait(driver, MAX)).until((ExpectedCondition<WebElement>) (WebDriver d) -> {
        return d.findElement(By.tagName("tbody"));
    });//w  ww.  j  a va2 s.co m
    Assert.assertThat(table.findElements(By.tagName("tr")).size(), is(6));

}

From source file:Practice01.java

public static void main(String[] args) throws InterruptedException {
    WebDriver driver = new FirefoxDriver();
    // Open Website
    driver.get("https://enterprise-demo.orangehrmlive.com/auth/login");
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    // Type Username
    driver.findElement(By.xpath("//input[@id='txtUsername']")).sendKeys("Admin");
    // Type Password
    driver.findElement(By.cssSelector("#txtPassword")).sendKeys("admin");
    // Click on Login
    driver.findElement(By.id("btnLogin")).click();
    // Click on PIM
    driver.findElement(By.id("menu_pim_viewPimModule")).click();
    // Click Add Employee
    driver.findElement(By.xpath("//a[@id='menu_pim_addEmployee']")).click();
    //        DateFormat dateFormat = new SimpleDateFormat("ddhhss");
    //        Date date = new Date();
    //        String date1 = dateFormat.format(date);
    //        String userName = "John"+date1;
    //        String username1= "Smith"+date1;
    // Type First name
    driver.findElement(By.id("firstName")).sendKeys("John");
    // Type Last name
    driver.findElement(By.id("lastName")).sendKeys("Smith");
    Random random = new Random();
    int eID = random.nextInt(1000);
    // Click on Location
    driver.findElement(By.id("employeeId")).clear();
    driver.findElement(By.id("employeeId")).sendKeys("" + eID);
    // Click on//  ww  w . j a  v  a  2 s . co m
    WebElement location_dd = driver.findElement(By.xpath("//select[@id='location']"));
    Select location = new Select(location_dd);
    location.selectByIndex(9);
    // Click Save
    driver.findElement(By.id("btnSave")).click();
    // Click on edit
    driver.findElement(By.xpath("//input[@value='Edit']")).click();
    // Click on licences expiry
    driver.findElement(By.id("personal_txtLicExpDate")).click();
    // Select date
    driver.findElement(By.xpath("html/body/div[4]/table/tbody/tr[5]/td[2]/a")).click();
    // Click on marital box
    driver.findElement(By.id("personal_cmbMarital")).click();
    // Click on married
    driver.findElement(By.xpath("//select[@name='personal[cmbMarital]']/option[3]")).click();
    // Click on Dob box
    driver.findElement(By.id("personal_DOB")).click();
    //click month box
    Select month_dd = new Select(driver.findElement(By.xpath("//div[@class='ui-datepicker-title']/select[1]")));
    month_dd.selectByVisibleText("Jul");
    // Select Year
    Select year_dd = new Select(driver.findElement(By.xpath("//div/select[2]")));
    year_dd.selectByIndex(35);
    // Click on Date
    driver.findElement(By.xpath("//table/tbody/tr[4]/td[4]/a")).click();
    // Click Save
    driver.findElement(By.id("btnSave")).click();
    // Verify Successfully Saved Message
    //Assert
    Assert.assertTrue("Successfully Saved",
            driver.findElement(By.xpath("html/body/div[1]/div[3]/div/div[2]/div[2]/div")).isEnabled());
    System.out.println("Successfully Saved");
    //assert1
    //        String expectedText = "Successfully Saved";
    //        String actualText = driver.findElement(By.xpath("html/body/div[1]/div[3]/div/div[2]/div[2]/div")).getText();
    //        Assert.assertEquals(expectedText,actualText);

    // Click Employee List
    driver.findElement(By.id("menu_pim_viewEmployeeList")).click();
    //        Thread.sleep(3000);
    //        //click on emp name
    //        driver.findElement(By.xpath("//input[@id='empsearch_employee_name_empName']")).sendKeys(userName + username1);

    //        // Type Employee ID
    //        driver.findElement(By.id("empsearch_id")).sendKeys(" "+eID);
    //        // Clear Employee ID
    //        driver.findElement(By.id("empsearch_id")).clear();
    Thread.sleep(2000);
    // Search Emp by ID
    driver.findElement(By.id("empsearch_id")).sendKeys("" + eID);
    // Search Employee By ID
    //        driver.findElement(By.id("searchBtn")).click();
    Thread.sleep(2000);
    driver.findElement(By.id("searchBtn")).click();

    //Assert
    Assert.assertTrue("John", driver.findElement(By.xpath("//tbody/tr/td[3]/a")).isEnabled());
    System.out.println("John");

    //
}

From source file:akori.Features.java

public static void traverse(Element e, int level, int index, String xpath, int siblings, PrintWriter writer,
        WebDriver driver) {

    if (siblings > 1) {
        WebElement webe = null;//w  w w.  j a v  a 2 s  . c  o m
        try {
            webe = driver.findElement(By.xpath("/" + xpath + "/" + e.nodeName() + "[" + index + "]"));
        } catch (Exception exp) {
            System.out.println("exception no se encontro");
        }
        if (webe != null) {
            Point po = webe.getLocation();
            Dimension d = webe.getSize();
            if (d.width <= 0 || d.height <= 0 || po.x < 0 || po.y < 0) {
                System.out.println("elemento negativo");
            } else {
                writer.println("/" + xpath + "/" + e.nodeName() + "[" + index + "]," + e.nodeName() + "," + po.x
                        + "," + po.y + "," + d.width + "," + d.height + "," + level);
            }
        }
    } else {
        WebElement webe = null;
        try {
            webe = driver.findElement(By.xpath("/" + xpath + "/" + e.nodeName()));
        } catch (Exception exp) {
            System.out.println("exception no se encontro");
        }
        if (webe != null) {
            Point po = webe.getLocation();
            Dimension d = webe.getSize();
            if (d.width <= 0 || d.height <= 0 || po.x < 0 || po.y < 0) {
                System.out.println("elemento negativo");
            } else {
                writer.println("/" + xpath + "/" + e.nodeName() + "," + e.nodeName() + "," + po.x + "," + po.y
                        + "," + d.width + "," + d.height + "," + level);
            }
        }
    }

    int childSize = e.children().size();
    Elements children = e.children();
    int i = 1;
    for (Element temp : children) {
        if (siblings > 1) {
            traverse(temp, level + 1, i, xpath + "/" + e.nodeName() + "[" + index + "]", childSize, writer,
                    driver);
        } else {
            traverse(temp, level + 1, i, xpath + "/" + e.nodeName(), childSize, writer, driver);
        }
        ++i;
    }

}

From source file:akori.SELENIUM.java

public static void main(String[] args) throws Exception {
    // The Firefox driver supports javascript 
    WebDriver driver = new FirefoxDriver();
    driver.manage().window().maximize();
    System.out.println(driver.manage().window().getSize().toString());
    System.out.println(driver.manage().window().getPosition().toString());
    URL = "http://www.mbauchile.cl";
    PATH = "E:\\NetBeansProjects\\AKORI\\1.png";

    // Go to the Google Suggest home page
    driver.get(URL);//from w  w  w .  j  av  a  2s . co  m

    // Enter the query string "Cheese"
    WebElement query = driver.findElement(By.id("container"));
    Point p = query.getLocation();
    Dimension d = query.getSize();

    System.out.println("x: " + p.x + " y: " + p.y);
    System.out.println("width: " + d.width + " height: " + d.height);

    driver.quit();
}

From source file:alwaysonline.Access.java

License:Open Source License

/**
 * Method for the access.//from w w  w . ja v  a 2s  .  c o m
 * @param user String that contains the username
 * @param pass String that contains the password
 * @return Access result: 0 failed, 1 success.
 */
public int doAccess(String user, String pass) {
    WebDriver driver = new HtmlUnitDriver();
    WebElement username, password;

    // access page
    driver.get("https://auth3.unipi.it/auth/perfigo_cm_validate.jsp");
    // looking for the credential fields
    username = driver.findElement(By.name("username"));
    username.clear();
    username.sendKeys(user);
    password = driver.findElement(By.name("password"));
    password.clear();
    password.sendKeys(pass);
    // Executing the access, submiting values
    driver.findElement(By.name("username")).submit();
    // If the session was already opened
    if (driver.getPageSource().contains("Too many users using this account")) {
        // put again credentials
        username = driver.findElement(By.name("username"));
        username.clear();
        username.sendKeys(user);
        password = driver.findElement(By.name("password"));
        password.clear();
        password.sendKeys(pass);
        // remove the old session
        driver.findElement(By.name("remove_old")).click();
        // submit again access data
        driver.findElement(By.name("username")).submit();
    }
    // controll if the access has been done successful
    if (driver.getPageSource().contains("You have been successfully logged on the network"))
        return 1;
    return 0;
}

From source file:AM.Physician.AddNewPhysician.java

public static WebElement txt_AP_PhysicianLastName(WebDriver driver) {
    element = driver.findElement(By.id("PhysicianLastName"));
    Waits.ForElementVisibility(driver, element);
    return element;
}

From source file:AM.Physician.AddNewPhysician.java

public static WebElement txt_AP_PhysicianFirstName(WebDriver driver) {
    element = driver.findElement(By.id("PhysicianFirstName"));
    Waits.ForElementVisibility(driver, element);
    return element;
}

From source file:AM.Physician.AddNewPhysician.java

public static WebElement txt_AP_PhysicianSuffix(WebDriver driver) {
    element = driver.findElement(By.id("PhysicianSuffix"));
    Waits.ForElementVisibility(driver, element);
    return element;
}

From source file:AM.Physician.AddNewPhysician.java

public static WebElement txt_AP_PhysicianUPIN(WebDriver driver) {
    element = driver.findElement(By.id("M0072"));
    Waits.ForElementVisibility(driver, element);
    return element;
}