Example usage for org.openqa.selenium By id

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

Introduction

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

Prototype

public static By id(String id) 

Source Link

Usage

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

License:Open Source License

/**
 * performs the find elements by id 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 .j a v a2  s  . c  o 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.id(searchParam));

    super.context.setElement(htmlElements);

    return FormatOutput.formatFindElementOutput(htmlElements, n);
}

From source file:bi.com.seleniumgrid.PhantomJsTest.java

License:Apache License

@Test
public void test() {
    final DesiredCapabilities capabilities = new DesiredCapabilities();
    // Configure our WebDriver to support JavaScript and be able to find the PhantomJS binary
    capabilities.setJavascriptEnabled(true);
    capabilities.setCapability("takesScreenshot", false);
    capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, PHANTOMJS_BINARY);
    final WebDriver driver = new PhantomJSDriver(capabilities);
    // Your test code here. For example:
    WebDriverWait wait = new WebDriverWait(driver, 30); // 30 seconds of timeout
    driver.get("https://en.wikipedia.org/wiki/Main_Page"); // navigate to Wikipedia

    pageTitle = driver.getTitle().trim();
    Assert.assertEquals(pageTitle, "GoalQuest");

    System.out.println("Page title is: " + driver.getTitle());

    By searchInput = By.id("searchInput"); // search for "Software"
    wait.until(ExpectedConditions.presenceOfElementLocated(searchInput));
    driver.findElement(searchInput).sendKeys("Software");
    By searchButton = By.id("searchButton");
    wait.until(ExpectedConditions.elementToBeClickable(searchButton));
    driver.findElement(searchButton).click();

    wait.until(ExpectedConditions.textToBePresentInElementLocated(By.tagName("body"), "Computer software")); // assert that the resulting page contains a text
}

From source file:bingbot.BingBot.java

/**
 * @param n number of searches to execute
 * @param file file to get user data from
 * @throws java.io.IOException/*  ww  w.j a  v  a  2  s .  c  o m*/
 * @throws java.lang.InterruptedException
 */
public void search(int m, int n, File file) throws Exception {
    System.out.println("Search method entered...");

    scanner = new Scanner(file);
    Scanner sc = new Scanner(new File("./files/dict"));

    String next = scanner.next();
    String name = null;
    String password = null;
    while (!next.equals("END")) {
        name = next;
        password = scanner.next();
        next = scanner.next();
    }

    for (int k = 0; k < m; k++) {

        driver.get("http://live.com");
        try {
            WebElement signout = driver.findElement(By.id("c_signout"));
            if (!signout.isDisplayed()) {
                return;
            }

        } catch (Exception e) {
            System.out.println("NO SUCH ELEMENT 'C_SIGNOUT'");
            System.out.println("SIGNING IN");

        }

        WebElement username = driver.findElement(By.id("i0116"));
        username.sendKeys(name);
        WebElement userpassword = driver.findElement(By.id("i0118"));
        userpassword.sendKeys(password);
        WebElement signin = driver.findElement(By.id("idSIButton9"));
        signin.click();
        Thread.sleep(10000);
        for (int j = 0; j < 41238; j++) {
            String word = sc.next();
            list[j] = word;
        }
        for (int i = 0; i < n; i++) {
            Random rand = new Random();
            driver.get("https://bing.com/");
            WebElement query = driver.findElement(By.id("sb_form_q"));
            query.sendKeys(list[rand.nextInt(41238)]);
            WebElement go = driver.findElement(By.id("sb_form_go"));
            go.click();
            Thread.sleep(rand.nextInt(5000) + 5000);
        }
    }
    seleniumServer.stop();
}

From source file:bingbot.BingBot.java

public void searchMobile(int m, int n, File file) throws Exception {
    System.out.println("Search method entered...");

    scanner = new Scanner(file);
    Scanner sc = new Scanner(new File("./files/dict"));

    String next = scanner.next();
    String name = null;/*w  w  w .  j ava2  s  . co m*/
    String password = null;
    while (!next.equals("END")) {
        name = next;
        password = scanner.next();
        next = scanner.next();
    }

    for (int k = 0; k < m; k++) {

        driver.get("http://live.com");
        try {
            WebElement signout = driver.findElement(By.linkText("Sign out"));
            signout.click();
        } catch (Exception e) {
            System.out.println("NO SUCH ELEMENT 'C_SIGNOUT'");
            System.out.println("SIGNING IN");

        }

        WebElement username = driver.findElement(By.id("i0116"));
        username.sendKeys(name);
        WebElement userpassword = driver.findElement(By.id("i0118"));
        userpassword.sendKeys(password);
        WebElement signin = driver.findElement(By.id("idSIButton9"));
        signin.click();
        for (int j = 0; j < 41238; j++) {
            String word = sc.next();
            list[j] = word;
        }
        for (int i = 0; i < n; i++) {
            Random rand = new Random();
            driver.get("https://bing.com/");
            WebElement query = driver.findElement(By.id("sb_form_q"));
            query.sendKeys(list[rand.nextInt(41238)]);
            WebElement go = driver.findElement(By.id("sbBtn"));
            go.click();
            Thread.sleep(rand.nextInt(5000) + 5000);
        }
    }
    seleniumServer.stop();
}

From source file:bodega.test.selenium.BodegaTest.java

@Test
public void testCrearBodega() throws Exception {
    webDriver.findElement(By.xpath("//button[contains(@id, 'createButton')]")).click();
    Thread.sleep(2000);/*from ww w. j  a v  a  2  s  .co  m*/

    webDriver.findElement(By.id("name")).clear();
    webDriver.findElement(By.id("name")).sendKeys("NuevaBodega");

    webDriver.findElement(By.xpath("//button[contains(@id, 'saveButton')]")).click();
    Thread.sleep(2000);
    List<WebElement> table = webDriver
            .findElements(By.xpath("//table[contains(@class,'table striped')]/tbody/tr"));
    boolean good = false;
    for (WebElement webElement : table) {
        List<WebElement> elements = webElement.findElements(By.xpath("td"));
        for (WebElement elemento : elements) {
            if (elemento.getText().equals("NuevaBodega"))
                good = true;
        }
    }

    assertTrue(good);
}

From source file:botski.selenium.SocialBot.java

License:Apache License

/**
 * @throws Exception // w  w w . j  a  v  a 2  s  .c om
 */
public void twitterLogin(String email, String password) throws Exception {
    browser.get("https://twitter.com/");
    WebElement formEmail = browser.findElement(By.id("signin-email"));
    formEmail.sendKeys(email);
    WebElement formPassword = browser.findElement(By.id("signin-password"));
    formPassword.sendKeys(password);
    formPassword.submit();
    String url = browser.getCurrentUrl();
    if (url.contains("/login/error")) {
        throw new Exception("Failed to login Twitter as '" + email + "' using password '" + password
                + "', I ended up here '" + url + "'");
    }
}

From source file:botski.selenium.SocialBot.java

License:Apache License

/**
 * @throws Exception /*from   w  w  w.j a va  2 s.  co  m*/
 */
public void pinterestLogin(String email, String password) throws Exception {
    browser.get("https://pinterest.com/login/?next=%2F");
    WebElement formEmail = browser.findElement(By.id("id_email"));
    formEmail.sendKeys(email);
    WebElement formPassword = browser.findElement(By.id("id_password"));
    formPassword.sendKeys(password);
    formPassword.submit();
    String url = browser.getCurrentUrl();
    if (url.contains("/login/")) {
        throw new Exception("Failed to login Pinterest as '" + email + "' using password '" + password
                + "', I ended up here '" + url + "'");
    }
}

From source file:br.com.dextraining.selenium.SeleniumTestCase.java

protected void esperarPor(String elementoDeId) throws InterruptedException {
    //Thread.sleep(2000); //apenas para podermos visualizar a tela antes da verificacao
    (new WebDriverWait(driver, TEMPO_MAXIMO_ESPERA)).until(new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver driver) {
            return driver.findElement(By.id(elementoDeId)).isDisplayed();
        }//  ww w  .  java 2s  .  c o  m
    });
}

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 w w.jav  a  2  s  .  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:br.ufmg.dcc.saotome.beholder.selenium.ui.SeleniumComponent.java

License:Apache License

@Override
public <T extends Component, Y extends T> List<T> loadByAttribute(Class<Y> type, final String IdFather,
        final String tagName, final String attributeName, final String value) {

    List<T> components = new ArrayList<T>();

    this.locator = new Locator(tagName, attributeName, value);

    if (this.isDisplayed) {
        WebDriverWait wait = new WebDriverWait(getSeleniumWebDriver(), TIMEOUT);

        wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.tagName(tagName)));
        List<WebElement> elements;

        if (IdFather == null) {
            elements = getSeleniumWebDriver().findElements(By.tagName(tagName));
        } else {/*from w w w  .j a  va 2  s  . c  o m*/
            elements = getSeleniumWebDriver().findElement(By.id(IdFather)).findElements(By.tagName(tagName));
        }

        for (WebElement el : elements) {
            if ((el.getAttribute(attributeName) != null)
                    && (el.getAttribute(attributeName).equalsIgnoreCase(value))) {
                T sc = null;

                try {
                    // Use of reflection for instantiate sc 
                    // this equivalent the get an instance of Builder.uiComponentBuilderInstance()
                    sc = (T) type.getDeclaredConstructor(WebDriver.class).newInstance(getSeleniumWebDriver());

                } catch (Exception e) {
                    e.printStackTrace();
                }

                ((SeleniumComponent) sc).setAttribute(attributeName, value);
                ((SeleniumComponent) sc).setElement(el);
                components.add(sc);
            }
        }
    }
    return components;
}