Example usage for org.openqa.selenium WebDriver findElements

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

Introduction

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

Prototype

@Override
List<WebElement> findElements(By by);

Source Link

Document

Find all elements within the current page using the given mechanism.

Usage

From source file:com.java.AppTestType_18_11_2015.java

public void MAKEOFFERBTN(WebDriver driver, String fieldText) throws IOException {

    String fieldType = fieldText;
    String field = fieldText;/*from   ww  w  .  j a  va 2  s . co m*/

    AppKeyWords cdf = AppKeyWords.valueOf(fieldType.toUpperCase());

    switch (cdf) {

    case VISITOR:
        try {

            List<WebElement> listings = driver.findElements(By.cssSelector("a[href*='/listing?listingid']"));

            for (int i = 0; i < listings.size(); i++) {

                WebElement requiredlisting = listings.get(i);
                requiredlisting.click();

                if (driver.findElements(By.cssSelector("input.primary_200")).size() != 0) {

                    System.out.println("Sign In to Make Offer button available");

                }
                resultDetails.setFlag(true);
            }

        } catch (StaleElementReferenceException e) {

            if (retries < MAX_STALE_ELEMENT_RETRIES) {

                retries++;
            }

            else {

                throw e;
            }

        } catch (Exception e) {

            resultDetails.setFlag(false);

            PrintWriter out = new PrintWriter(
                    new BufferedWriter(new FileWriter("C:/TestProject - DEV/Log File/logfile.txt", true)));
            FileOutputStream fos = new FileOutputStream("C:/TestProject - DEV/Log File/logfile.txt");
            PrintStream ps = new PrintStream(fos);
            e.printStackTrace(ps);

        }

        break;

    }

}

From source file:com.java.AppTestType_18_11_2015.java

public void ASKQUESTION(WebDriver driver, String fieldText, String value) {

    try {/*from   ww w.  jav a2  s.  c  o  m*/
        int i;
        List<WebElement> listings = driver.findElements(By.cssSelector("a[href*='/listing?listingid']"));

        for (i = 0; i < listings.size(); i++) {

            WebElement requiredlisting = listings.get(i);
            requiredlisting.click();

            getvalue = driver.findElement(By.xpath("//div[7]/span")).getText();
            System.out.println(getvalue);

            driver.findElement(By.xpath("//div[3]/div[2]/input")).click();
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            ;

            driver.findElement(By.id("listingQuestion")).click();
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            ;
            driver.findElement(By.id("listingQuestion")).sendKeys("where is the listing located");
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            ;
            driver.findElement(By.name("submitq")).click();
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            ;

            driver.findElement(By.xpath("//div/div[2]/div[3]/input")).click();
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            ;

            driver.findElement(By.cssSelector("span")).click();

            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            ;
            driver.findElement(By.linkText("Sign Out")).click();
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            ;
            driver.manage().deleteAllCookies();
            driver.navigate().refresh();
            driver.get(fieldText);
            driver.manage().deleteAllCookies();
            driver.findElement(By.id("aurid")).sendKeys("mglaz@assetnation.com");

            driver.findElement(By.id("apwd")).sendKeys("Equipment1$");
            driver.findElement(By.xpath("//input[@value='Login']")).click();

            driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

            driver.findElement(By.id("userAgreementBtn")).click();

            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

            System.out.println(getvalue);
            driver.findElement(By.id("quicksearch")).sendKeys(getvalue);
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            ;
            driver.findElement(By.cssSelector("input.quicksearch_go")).click();

            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

            driver.findElement(By.linkText("Lot Q/A")).click();

            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            ;

            String ParentHandle = driver.getWindowHandle();
            driver.findElement(By.linkText("Forward")).click();
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            ;
            for (String winHandle : driver.getWindowHandles()) {

                System.out.println("Entered For Loop");
                driver.switchTo().window(winHandle);
            }

            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            ;
            driver.findElement(By.xpath("//td/input[4]")).click();
            driver.findElement(By.id("id_button_forward")).click();

            driver.findElement(By.id("id_button_forward_seller_location")).click();

            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            ;

            driver.switchTo().alert().accept();
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            ;
            driver.switchTo().window(ParentHandle);
            driver.findElement(By.xpath("//ul/ul/li/a")).click();

            driver.findElement(By.xpath("//td[2]/a")).click();
            String seller = driver.findElement(By.id("id_frm_Email_Address")).getAttribute("value");

            driver.findElement(By.linkText("Logout")).click();
            driver.manage().deleteAllCookies();
            driver.navigate().refresh();
            driver.get(value);
            driver.findElement(By.id("sign")).click();

            driver.findElement(By.id("loginfield")).sendKeys(seller);
            driver.findElement(By.id("pwdfield")).sendKeys("Equipment1$");
            driver.findElement(By.cssSelector("input.secondary_110")).click();
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            ;
            driver.findElement(By.linkText("MYONE")).click();
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

            resultDetails.setFlag(true);

        }

    } catch (Exception e) {

        resultDetails.setFlag(false);
    }

}

From source file:com.java.AppTestType_18_11_2015.java

public void LITEUSERREGISTRATION(WebDriver driver, String fieldText, String value) {
    //Lite user registration

    try {//from  ww w .j  a  v a 2 s  .  c o  m
        driver.manage().deleteAllCookies();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.get(fieldText);

        String pwd = "Equipment1$";

        String phone = "";
        String Email = "";
        String Fname = value;
        String lname = "Test";
        String validation = "";
        long inc = Math.round(Math.random() * 100000); // generates 5 digit number.
        String ID = "@yahoo.in";

        Random random = new Random();
        long dphone = Math.round(random.nextFloat() * Math.pow(10, 12));

        Email = Fname + lname + inc + ID;

        driver.findElement(By.id("emailaddress")).sendKeys(Email);
        System.out.println(Email);

        phone = Long.toString(dphone);

        driver.findElement(By.id("firstname")).sendKeys(Fname);
        driver.findElement(By.id("lastname")).sendKeys(lname);
        driver.findElement(By.id("phone")).sendKeys(phone);
        driver.findElement(By.id("newPassword")).sendKeys(pwd);
        driver.findElement(By.id("confirmPassword")).sendKeys(pwd);
        driver.findElement(By.xpath("//button[text()='JOIN NOW']")).click();
        Thread.sleep(10000);

        //condition to check the email is already exist
        if (driver.findElements(By.xpath("//*[@class='error-txt']")).size() > 0) {

            //If the email" Email Address already exists"
            if (driver.findElement(By.xpath("//*[@class='error-txt']")).getText()
                    .contains("Email Address already exists in the system")) {
                inc = inc++;
                Email = Fname + lname + inc + "test" + ID;
                driver.findElement(By.id("emailaddress")).clear();
                driver.findElement(By.id("emailaddress")).sendKeys(Email);
                driver.findElement(By.xpath("//button[text()='JOIN NOW']")).click();
            }

            // If the phone number already exist
            if (driver.findElement(By.xpath("//*[@class='error-txt']")).getText()
                    .contains("Another user has the same daytime phone number")) {
                phone = phone + random.nextInt(9);
                driver.findElement(By.id("phone")).clear();
                driver.findElement(By.id("phone")).sendKeys(phone);
                driver.findElement(By.xpath("//button[text()='JOIN NOW']")).click();

            }
        }

        //Verifying that user is successfully registered
        WebDriverWait wait = new WebDriverWait(driver, 35);
        wait.until(ExpectedConditions
                .visibilityOfElementLocated(By.xpath("//*[@class='fa fa-user famember-icon']"))); //verifying the "Activate your membership" icon 
        System.out.println("Successfully Registered user with mail id: " + Email);
        EMAIL = Email;

        resultDetails.setFlag(true);

    }

    catch (Exception e) {
        resultDetails.setFlag(false);

        e.printStackTrace();

    }

}

From source file:com.java.AppTestType_18_11_2015.java

public void AGREETERMS(WebDriver driver) {
    try {/*w  ww.ja va  2 s. c  o  m*/
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        // System.out.println("size is:"+driver.findElements(By.xpath("//input[@id='useragreetoterms' and @type='checkbox']")).size());suman
        if (driver.findElements(By.xpath("//input[@id='useragreetoterms' and @type='checkbox']")).size() > 0) {
            driver.findElement(By.xpath("//input[@id='useragreetoterms' and @type='checkbox']")).click();
            driver.findElement(By.xpath("//*[@class='btn btn-primary btn-group-justified' and @type='submit']"))
                    .click();
            driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

        }
        resultDetails.setFlag(true);
    } catch (Exception e) {
        resultDetails.setFlag(false);
        e.printStackTrace();
    }
}

From source file:com.java.AppTestType_18_11_2015.java

public boolean SEARCH_RESULT_COMPARISON(WebDriver driver) {
    try {/*  w  ww.j a  va  2 s.  com*/
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        String msg = driver.findElement(By.id("e1SitesFound")).getText();
        driver.findElement(By.linkText("Source Site")).click();
        Thread.sleep(2500);

        if (msg.contains("Ritchie Bros"))
            driver.findElement(By.xpath("(//*[@id='site_id:RBA']/span[1]/input)[2]")).click();
        Thread.sleep(1500);
        if (msg.contains("SalvageSale"))
            driver.findElement(By.xpath("(//*[@id='site_id:SS']/span[1]/input)[2]")).click();

        Thread.sleep(5000);

        //String E1Count = driver.findElement(By.cssSelector("span.e1Count")).getText();//
        System.out.println("size1 issssssssssssssssssss: "
                + driver.findElements(By.xpath("(//*[@id='site_id:E1']/span[3])[2]")).size());
        String E1Count = driver.findElement(By.xpath("(//*[@id='site_id:E1']/span[3])[2]")).getText();
        System.out.println(E1Count);
        String EOCountt = E1Count.substring(E1Count.indexOf("(") + 1, E1Count.lastIndexOf(")"));
        Integer count1 = Integer.parseInt(EOCountt);
        System.out.println("count1 is: " + count1);

        Thread.sleep(5000);
        System.out.println("size2 isssssssssssssssssssssss" + driver.findElements(By.id("e1NumFound")).size());
        System.out.println(
                "size3 isssssssssssssssssssssss" + driver.findElements(By.id("e1ResultsFound")).size());
        String listingcount = driver.findElement(By.id("e1NumFound")).getText();
        String nextlistingcount = driver.findElement(By.id("e1ResultsFound")).getText();

        System.out.println(listingcount);
        System.out.println(nextlistingcount);
        //suman
        boolean lotDisplay = driver.findElement(By.id("e1NumFound")).isDisplayed();
        boolean eventDisplay = driver.findElement(By.id("e1ResultsFound")).isDisplayed();
        boolean auctionDisplay = driver.findElement(By.id("e1AuctionsFound")).isDisplayed();
        //System.out.println(nextlistingcount);
        String listingcount2 = "";
        int convlistingcount = 0;
        if (lotDisplay) {
            listingcount2 = listingcount.replace(",", "");
            convlistingcount = Integer.parseInt(listingcount2);
        }

        String nextlistingcount2 = "";
        int convnextlistingcount = 0;
        if (eventDisplay) {
            nextlistingcount2 = nextlistingcount.replace(",", "");
            convnextlistingcount = Integer.parseInt(nextlistingcount2);

        }

        String auctionCount = "";
        int auctionCount2 = 0;
        if (auctionDisplay) {
            auctionCount = driver.findElement(By.id("e1AuctionsFound")).getText().replace(",", "");
            auctionCount2 = Integer.parseInt(auctionCount);

        }

        System.out.println("lot count is: " + convlistingcount);
        System.out.println("event count is: " + convnextlistingcount);
        System.out.println("auction count is: " + auctionCount2);

        Integer myfinalcount = convlistingcount + convnextlistingcount + auctionCount2;
        String compfinalcount = String.valueOf(myfinalcount);

        System.out.println(count1);
        System.out.println(myfinalcount);

        if (count1 <= 0 || (!count1.equals(myfinalcount)))
            return false;

        else
            return true;

    } catch (Exception e) {
        return false;
    }
}

From source file:com.java.AppTestType_18_11_2015.java

public void SIGNIN(WebDriver driver, String fieldText, String value) {

    try {/*from   ww  w.ja va2s  .co m*/

        try {
            WebDriverWait wait = new WebDriverWait(driver, 20);
            // wait.until(ExpectedConditions.titleIs("Used Equipment Sales | Buy & Sell Online | Ritchie Bros. EquipmentOne"));

            driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
            driver.manage().deleteAllCookies();
            //driver.findElement(By.linkText("Sign In")).click();
            driver.findElement(By.name("josso_username")).clear();
            driver.findElement(By.name("josso_username")).sendKeys(fieldText);
            driver.findElement(By.name("josso_password")).clear();
            driver.findElement(By.name("josso_password")).sendKeys("Equipment1$");
            try {
                driver.findElement(By.xpath("//*[@value='SIGN IN']")).click();
            } catch (Exception elementnotfoundexException) {
                driver.findElement(By.xpath("//button[text()='SIGN IN']")).click();
            }
            System.out.println("size is   :" + driver.findElements(By.id("iduser")).size());
            if (driver.findElements(By.id("iduser")).size() > 0) {
                System.out.println("1111111111");
                driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
                if (driver.findElements(By.xpath("//input[@id='useragreetoterms' and @type='checkbox']"))
                        .size() > 0) {
                    driver.findElement(By.xpath("//input[@id='useragreetoterms' and @type='checkbox']"))
                            .click();
                    driver.findElement(
                            By.xpath("//*[@class='btn btn-primary btn-group-justified' and @type='submit']"))
                            .click();
                    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
                    // driver.findElement(By.xpath("//input[@value='Go to EquipmentOne']")).click();
                    //  wait.until(ExpectedConditions.titleIs("Used Equipment Sales | Buy & Sell Online | Ritchie Bros. EquipmentOne"));
                }
                //to confirm successful sign in as valid user,verifying the name displayed next to "Hello" text

                if (driver.findElement(By.id("iduser")).getText().equalsIgnoreCase(value))
                    System.out.println("Successfully Logged in");
                else {
                    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
                    driver.findElement(By.linkText("Sign Out")).click();
                    wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.name("josso_password")));
                    driver.findElement(By.name("josso_username")).clear();
                    driver.findElement(By.name("josso_username")).sendKeys(fieldText);
                    driver.findElement(By.name("josso_password")).clear();
                    driver.findElement(By.name("josso_password")).sendKeys("Equipment1$");
                    driver.findElement(By.xpath("//*[@value='SIGN IN']")).click();
                    wait.until(ExpectedConditions.elementToBeClickable(By.id("iduser")));
                    //agree terms-This piece of code is to verify agree terms checkbox after re logging(not logged in as the valid user)
                    driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
                    if (driver.findElements(By.xpath("//input[@id='useragreetoterms' and @type='checkbox']"))
                            .size() > 0) {
                        driver.findElement(By.xpath("//input[@id='useragreetoterms' and @type='checkbox']"))
                                .click();
                        driver.findElement(By
                                .xpath("//*[@class='btn btn-primary btn-group-justified' and @type='submit']"))
                                .click();
                        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
                        // driver.findElement(By.xpath("//input[@value='Go to EquipmentOne']")).click();
                        //  wait.until(ExpectedConditions.titleIs("Used Equipment Sales | Buy & Sell Online | Ritchie Bros. EquipmentOne"));
                    }

                }
                resultDetails.setFlag(true);
            }

        }

        //This catch is used to catch the error "You are NOT authorized to access Site" in E1 Login//
        /////////////////////////////////////
        catch (Exception e1) {

            driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
            if (driver.getPageSource().contains("You are NOT authorized to access Site")) {
                System.out.println("You are NOT authorized to access Site error message is displayed");
                driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
                driver.navigate().to("http://e1.dev.assetnation.com");
                SIGNIN(driver, fieldText, value);
            }

        }
    }

    catch (Exception e) {
        e.printStackTrace();
        resultDetails.setFlag(false);
    }

}

From source file:com.java.AppTestType_18_11_2015.java

public void WAITFORTHIS(WebDriver driver, String fieldText, String value) {
    //Used to verify the text in any specific Loctor or Message
    //<XPH or LNK or IDI or TXT or MSG><Locator> in fieldText 
    //Time in milliseconds in Value
    try {/*from  w  w w  .  j  a v a2  s  .c  o  m*/

        String f = fieldText;
        String time = value;
        String locatorType = f.substring(0, 3);
        String locatorValue = f.substring(3, f.length());
        int timevalue = Integer.parseInt(time);
        driver.manage().timeouts().implicitlyWait(timevalue, TimeUnit.MILLISECONDS);

        if (locatorType.equalsIgnoreCase("XPH")) {

            if (driver.findElements(By.xpath(locatorValue)).size() > 0)
                resultDetails.setFlag(true);
        } else if (locatorType.equalsIgnoreCase("LNK")) {

            if (driver.findElements(By.linkText(locatorValue)).size() > 0)
                resultDetails.setFlag(true);

        } else if (locatorType.equalsIgnoreCase("IDI") || locatorType.equalsIgnoreCase("TXT")) {

            if (driver.findElements(By.id(locatorValue)).size() > 0)
                resultDetails.setFlag(true);

        } else if (locatorType.equalsIgnoreCase("MSG")) {

            int j = 0;
            int t = timevalue / 1000;
            for (int i = 0; i < t; i++) {
                if (driver.findElement(By.xpath("//*[contains(text(),'" + locatorValue + "')]"))
                        .isDisplayed()) {
                    j = 1;
                    break;
                }
                Thread.sleep(1000);

            }
            if (j == 1)
                resultDetails.setFlag(true);

        } else if (locatorType.equalsIgnoreCase("PLK")) {

            if (driver.findElements(By.partialLinkText(locatorValue)).size() > 0)
                resultDetails.setFlag(true);

        }

    }

    catch (Exception e) {
        resultDetails.setErrorMessage(fieldText + "Locator Not Found");
        resultDetails.setFlag(false);
        e.printStackTrace();
    }
}

From source file:com.java.AppTestType_18_11_2015.java

public void MAXBIDFROMEVENTPAGE(WebDriver driver) {

    try {//from ww w  . j av a 2 s.com
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        List<WebElement> events = driver
                .findElements(By.xpath("//a[@class='jsTitle' and contains(@href,'/event?eventid')]"));
        System.out.println("event size is: " + events.size());
        Random r = new Random();
        /*int randomValue = r.nextInt(listings.size()); //Getting a random value that is between 0 and (list's size)-1
        */ WebDriverWait wait = new WebDriverWait(driver, 20);

        //selecting random event and trying to bid on first listing, if the event doesn't have open listings it will try with next event
        for (int i = 0; i < events.size(); i++) {
            events.get(r.nextInt(events.size())).click();
            Thread.sleep(8000);
            if (driver.findElement(By.xpath("(//*[contains(@value,'PLACE A BID')])[2]")).isDisplayed()) {
                driver.findElement(By.xpath("(//*[contains(@value,'PLACE A BID')])[2]")).click();
                BIDDING_IN_LISTINGDETAILSPAGE(driver);
                resultDetails.setFlag(true);
                break;
            }
            //go back to event page
            else
                driver.navigate().back();

        }

    } catch (Exception e) {

        e.printStackTrace();
        resultDetails.setErrorMessage("Listing is not available or Closed");
        resultDetails.setFlag(false);

    }

}

From source file:com.java.AppTestType_18_11_2015.java

public void EXACTBIDFROMEVENTPAGE(WebDriver driver) {

    try {/*from   w w  w  . j  a  v  a 2s  .co m*/
        System.out.println("Enterd to EXACTBIDFROMEVENTPAGE ");
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        List<WebElement> events = driver
                .findElements(By.xpath("//a[@class='jsTitle' and contains(@href,'/event?eventid')]"));
        System.out.println("event size is: " + events.size());
        Random r = new Random();
        /*int randomValue = r.nextInt(listings.size()); //Getting a random value that is between 0 and (list's size)-1
        */ WebDriverWait wait = new WebDriverWait(driver, 20);

        //selecting random event and trying to bid on first listing, if the event doesn't have open listings it will try with next event
        for (int i = 0; i < events.size(); i++) {
            events.get(r.nextInt(events.size())).click();
            Thread.sleep(8000);
            //wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("(//*[contains(@value,'PLACE A BID')])[2]"))));
            if (driver.findElement(By.xpath("(//*[contains(@value,'PLACE A BID')])[2]")).isDisplayed()) {
                driver.findElement(By.xpath("(//*[@type='radio' and @value='Exact Bid'])[2]")).click();
                Thread.sleep(4000);
                driver.findElement(By.xpath("(//*[contains(@value,'PLACE A BID')])[2]")).click();
                BIDDING_IN_LISTINGDETAILSPAGE(driver);
                resultDetails.setFlag(true);
                break;
            }
            //go back to event page
            else
                driver.navigate().back();

        }

    } catch (Exception e) {

        e.printStackTrace();
        resultDetails.setErrorMessage("Listing is not available or Closed");
        resultDetails.setFlag(false);

    }

}

From source file:com.java.AppTestType_18_11_2015.java

public void E1LISTINGSELECTION(WebDriver driver, String value) throws InterruptedException {

    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    int k = 0;//  www .  ja  va 2  s .  co  m
    for (int i = 0; i < 10; i++) {
        if (driver
                .findElements(
                        By.xpath("(//a[@class='jsImageLink' and contains(@href,'/listing?listingid')])[1]"))
                .size() > 0) {
            if (value.equalsIgnoreCase("CLK"))
                driver.findElement(
                        By.xpath("(//a[@class='jsImageLink' and contains(@href,'/listing?listingid')])[1]"))
                        .click();
            k = 1;
            break;
        } else {
            driver.findElement(By.xpath("(//*[text()='Next Page'])[1]")).click();

        }

        Thread.sleep(10000);
    }
    if (k == 1)
        resultDetails.setFlag(true);

}