Example usage for org.openqa.selenium By className

List of usage examples for org.openqa.selenium By className

Introduction

In this page you can find the example usage for org.openqa.selenium By className.

Prototype

public static By className(String className) 

Source Link

Document

Find elements based on the value of the "class" attribute.

Usage

From source file:au.org.theark.test.integration.container.ITestLoginPage.java

License:Open Source License

@Test
public void testInvalidLogin() {
    log.info("Starting test " + new Object() {
    }.getClass().getEnclosingMethod().getName());
    driver.findElement(By.name("userName")).sendKeys("arksuperuser@ark.org.au");
    driver.findElement(By.name("password")).sendKeys("Incorrect Password");
    driver.findElement(By.name("signInButton")).click();

    //If the "Invalid username and password" warning comes up, then we didn't log in.
    element = driver.findElement(By.className("feedbackPanelERROR"));
    TestCase.assertNotNull(element);// w ww  .j  a  va 2s  .  c  o  m
    TestCase.assertEquals("Invalid username and/or password.", element.getText());
    log.info("Ending test " + new Object() {
    }.getClass().getEnclosingMethod().getName());
}

From source file:automated_tests.InitialPopups.java

@Test(priority = 1)
public void rejectConsentToUse() {
    driver.findElement(By.id("btnDoNotConsent")).click();

    //TODO: Look into $.tablesorter is undefined Javascript error
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("no_html5")));

    try {//from ww w .ja v  a2s  . com
        assertEquals("This application can only be used after accepting the Terms of Use and Privacy Policy",
                driver.findElement(By.cssSelector("span")).getText());
        System.out.println("Rejected terms of use warning text correct");
    } catch (Error e) {
        verificationErrors.append(e.toString());
    }
}

From source file:automated_tests.InitialPopups.java

@Test(priority = 3)
public void rejectTermsOfUse() {
    driver.findElement(By.id("btnRejectTerms")).click();
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("no_html5")));

    try {//from w w  w.ja  v  a  2s .c o  m
        assertEquals("This application can only be used after accepting the Terms of Use and Privacy Policy",
                driver.findElement(By.cssSelector("span")).getText());
        System.out.println("Rejected terms of use warning text correct");
    } catch (Error e) {
        verificationErrors.append(e.toString());
    }
}

From source file:automated_tests.InitialPopups.java

@Test(priority = 5)
public static void clickInstructionsOverlay() {
    //Wait until the instructions overlay to show up before clicking it

    wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("instructionsOverlayHeader")));
    driver.findElement(By.className("instructionsOverlayHeader")).click();

    //Wait until the instructions overlay disappears before continuing
    wait.until(ExpectedConditions.invisibilityOfElementLocated(By.className("instructionsOverlayHeader")));
    System.out.println("Closed instructions overlay");
}

From source file:be.ugent.mmlab.webdriver.Demo.java

License:Apache License

public void demoTime() throws InterruptedException {
    // initialize web browser.
    WebDriver driver = new FirefoxDriver();

    // go to the Belgian railways website
    driver.get("http://www.belgianrail.be");

    // select "English"
    // HTML:/*from   ww w  .  j a  v  a2s. com*/
    // <a
    //   id="ctl00_bodyPlaceholder_languagesList_ctl02_languageNameLink"
    //   href="javascript:__doPostBack('ctl00$bodyPlaceholder$languagesList$ctl02$languageNameLink','')"
    // > English </a>
    WebElement english = driver
            .findElement(By.id("ctl00_bodyPlaceholder_languagesList_ctl02_languageNameLink"));
    english.click();

    // fill out departure
    WebElement from = driver.findElement(By.id("departureStationInput"));
    from.sendKeys("Gent-Dampoort");

    // pause for a second to make it not too fast
    Thread.sleep(1000);
    // click in the field to get the auto-completion away
    from.click();

    // fill out arrival
    WebElement to = driver.findElement(By.id("arrivalStationInput"));
    to.sendKeys("Brussel-Noord");

    Thread.sleep(1000);
    to.click();

    // click timetable button
    WebElement timetableButton = driver.findElement(By.id(
            "ctl00_ctl00_bodyPlaceholder_bodyPlaceholder_mobilityTimeTableSearch_HomeTabTimeTable1_submitButton"));
    timetableButton.click();

    // get departure info
    // HTML:
    // <td headers="hafasOVTimeOUTWARD" class="time">
    //    <div>
    //     <div class="planed overviewDep">
    //      10:00 dep <span class="bold prognosis">+12 min.</span>
    //     </div>
    //     <div class="planed">
    //      11:20 arr <span class="bold green">+0 min.</span>
    //     </div>
    //   </div>
    // </td>
    List<WebElement> timeCells = driver.findElements(By.className("time"));
    for (WebElement timeCell : timeCells) {
        List<WebElement> times = timeCell.findElements(By.className("planed"));
        System.out.println("----------------------------------------------");
        System.out.println("departure time: " + times.get(0).getText());
        System.out.println("arrival time:   " + times.get(1).getText());
    }
}

From source file:beseenium.model.action.findElementsBy.FindElementsByClass.java

License:Open Source License

/**
 * performs the find elements by class action.
 * @param n the index of the element to find information on, i.e. if 3 results are found
 * the 0 will be the first element 1 the second and so on. will get an array out of bounds.
 * If you wish the action to return all of the results found then set n = -1.
 * @return String representation of the returnParam set in the ActionData object
 * passed into the constructor.//from w  w w. jav a  2 s .co m
 * @throws ActionDataException  
 */
@Override
public String execute(int n) throws ActionDataException {
    String searchParam = super.context.getInputParam();
    WebDriver browser = super.context.getDriver();
    List<WebElement> htmlElements = browser.findElements(By.className(searchParam));

    super.context.setElement(htmlElements);

    return FormatOutput.formatFindElementOutput(htmlElements, n);
}

From source file:botski.example.AddMeFastExample.java

License:Apache License

public void run() {
    try {/*w  ww.  jav a 2s. c  o  m*/
        int likes = 0;
        int points = 0;
        long start_time = System.currentTimeMillis();

        // Setup Selenium
        driver = new FirefoxDriver();
        jse = (JavascriptExecutor) driver;

        // Perform the Login actions
        facebookLogin();
        addMeFastLogin();

        // Goto the Facebook Likes page
        driver.get("http://addmefast.com/free_points/facebook_likes.html");
        if ("http://addmefast.com/free_points/facebook_likes.html".equals(driver.getCurrentUrl()) == false) {
            System.err.println(
                    "I was trying to navigate to 'http://addmefast.com/free_points/facebook_likes.html' and ended up here '"
                            + driver.getCurrentUrl() + "'");
            return;
        }

        // Remember the main window handle
        String windowHandle = (String) driver.getWindowHandles().toArray()[0];

        // Go into the loop
        while (true) {
            // Reacts to the appearance of a Like button
            (new WebDriverWait(driver, 60)).until(new ExpectedCondition<Boolean>() {
                public Boolean apply(WebDriver d) {
                    return (Boolean) jse.executeScript(
                            "if(jQuery('.single_like_button').length){return true;}else{return false;}");
                }
            });

            // Click the like button by injecting JavaScript to the page
            jse.executeScript("jQuery('.single_like_button').click();");

            // Allow time for the popup window to appear
            Utils.sleep(1000);

            // Switch to the new window
            Set<String> winSet = driver.getWindowHandles();
            List<String> winList = new ArrayList<String>(winSet);
            String newTab = winList.get(winList.size() - 1);
            driver.switchTo().window(newTab); // switch to new tab

            // Click the first "Like" button on the page
            jse.executeScript(
                    "var inputs=document.getElementsByTagName('input');for(var i=0; i<inputs.length; i++){var input=inputs[i];var value=input.getAttribute('value');if(value!=null){if(value=='Like'){input.click();break;}}}");

            // Allow time for the Like action to go through
            Utils.sleep(1000);

            // Close this window and switch back to the main one
            driver.close();
            driver.switchTo().window(windowHandle);

            // This delay allows AddMeFast to detect the window close and request the next page to Like
            Utils.sleep(5000);

            // Update counters
            likes++;
            WebElement points_count = driver.findElement(By.className("points_count"));
            try {
                points = Integer.parseInt(points_count.getText());
            } catch (Exception ignore) {
            }
            long diff = (System.currentTimeMillis() - start_time) / 1000;
            System.out.println("" + likes + " likes, " + points + " points in " + diff + " seconds");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:botski.example.PinterestExample.java

License:Apache License

public void run() {
    try {//from w ww .  j a va  2  s.c o  m
        // Set your Proxy here
        useProxy("123.123.123.123", 8080);

        // Default to Firefox, but could use Chrome
        initializeFirefox();

        // Login and goto the homepage
        pinterestLogin("email@address.com", "password");

        // Search the DOM to find all visible Like buttons
        List<WebElement> list = browser.findElements(By.className("likebutton"));
        System.out.println("Found " + list.size() + " 'Like' buttons!");

        // Randomly 'Like' ~ 10% of them
        int count = 0;
        for (WebElement element : list) {
            if (Math.random() <= 0.10) // roughly 10%
            {
                // Click the 'Like' button using Javascript
                javascript.executeScript("arguments[0].click();", element);
                System.out.println("   ... Liked pin " + element.getAttribute("data-id"));
                count++;
            }
        }
        System.out.println("Liked " + count + " pins!");

        // Sleep for 5 seconds then quit
        sleep(5000);
        browser.quit();
    } catch (Exception e) {
        System.err.println("Something has gone horribly wrong!");
        e.printStackTrace();
    }
}

From source file:br.gov.frameworkdemoiselle.behave.runner.webdriver.util.ByConverter.java

License:Open Source License

public static By convert(ElementLocatorType type, String locator) {
    By by = null;/*from   w  ww .j a  v a  2s  . c  o  m*/

    if (type == ElementLocatorType.Id) {
        by = By.id(locator);
    } else if (type == ElementLocatorType.ClassName) {
        by = By.className(locator);
    } else if (type == ElementLocatorType.CssSelector) {
        by = By.cssSelector(locator);
    } else if (type == ElementLocatorType.LinkText) {
        by = By.linkText(locator);
    } else if (type == ElementLocatorType.Name) {
        by = By.name(locator);
    } else if (type == ElementLocatorType.TagName) {
        by = By.tagName(locator);
    } else if (type == ElementLocatorType.XPath) {
        by = By.xpath(locator);
    } else if (type == ElementLocatorType.Value) {
        by = By.xpath("//*[@value='" + locator + "']");
    } else {
        throw new BehaveException(message.getString("exception-invalid-option", type, "convert"));
    }

    return by;
}

From source file:bst.cpo.automation.dm.actions.Settings_Actions.java

public void My_Profile_Elements() throws InterruptedException {
    //Looks like this just returns the first element found with the name.
    WebElement element = DMDriver.findElement(By.className("pure-control-group"));
    logThis(element.getText());/*from  ww  w  .  j  a  v a  2s . c  o m*/
    logThis("===========");
    //While this print out each individually.
    //TODO - find away to loop through without specifically doing each index?
    List<WebElement> we2 = DMDriver.findElements(By.className("pure-control-group"));
    logThis("(0)=" + we2.get(0).getText());
    logThis("(1)=" + we2.get(1).getText());
    logThis("(2)=" + we2.get(2).getText());
    logThis("(3)=" + we2.get(3).getText());
}