Example usage for org.openqa.selenium By linkText

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

Introduction

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

Prototype

public static By linkText(String linkText) 

Source Link

Usage

From source file:com.example.selenium.grid.SeleniumGridTest.java

@Test
public void testLogin() throws InterruptedException {

    WebElement userNameText = driver.findElement(By.name("email"));
    userNameText.sendKeys("jayasimha.bj@gmail.com");

    WebElement passwordText = driver.findElement(By.id("password"));
    passwordText.sendKeys("welcome123");

    WebElement loginButton = driver.findElement(By.linkText("Login"));
    loginButton.click();//from  ww w .  j  a v a  2s . com
    if (driver.getTitle().equals("SB Admin 2 - Bootstrap Admin Theme")) {
        assertTrue(true);
    } else {
        assertTrue(false);
    }
}

From source file:com.example.tests.CadastroChromeTest.java

@Test
public void testCadastroLol() throws Exception {

    GerarCod gc = new GerarCod();

    driver.get("http://localhost:8080/" + "/ModuloDois/");
    driver.findElement(By.linkText("Cadastrar")).click();
    driver.findElement(By.name("campoCadastroID")).clear();
    driver.findElement(By.name("campoCadastroID")).sendKeys(gc.gerarCod());
    driver.findElement(By.name("campoCadastroNome")).clear();
    driver.findElement(By.name("campoCadastroNome")).sendKeys("exemplo");
    driver.findElement(By.name("campoCadastroQuantidade")).clear();
    driver.findElement(By.name("campoCadastroQuantidade")).sendKeys("78");
    driver.findElement(By.name("campoCadastroUnidade")).clear();
    driver.findElement(By.name("campoCadastroUnidade")).sendKeys("78");
    driver.findElement(By.name("campoCadastroValor")).clear();
    driver.findElement(By.name("campoCadastroValor")).sendKeys("20.32");
    driver.findElement(By.cssSelector("button.btn.btn-success")).click();
}

From source file:com.gargoylesoftware.htmlunit.CookieManager4Test.java

License:Apache License

/**
 * @throws Exception if the test fails//from   w w w  .ja  v a2s  . com
 */
@Test
public void domainDuplicateLeadingDotSend() throws Exception {
    final String html = "<html><body>\n" + "<a href='next.html'>next page</a>\n" + "</body></html>";

    final List<NameValuePair> responseHeader = new ArrayList<>();
    responseHeader.add(new NameValuePair("Set-Cookie", "c1=1; Domain=host1." + DOMAIN + "; Path=/"));
    responseHeader.add(new NameValuePair("Set-Cookie", "c2=2; Domain=.host1." + DOMAIN + "; Path=/"));

    getMockWebConnection().setDefaultResponse(html, 200, "Ok", "text/html", responseHeader);

    final WebDriver webDriver = getWebDriver();
    webDriver.manage().deleteAllCookies();

    loadPageWithAlerts2(new URL(URL_HOST1));
    WebRequest lastRequest = getMockWebConnection().getLastWebRequest();
    assertNull(lastRequest.getAdditionalHeaders().get("Cookie"));

    webDriver.findElement(By.linkText("next page")).click();
    lastRequest = getMockWebConnection().getLastWebRequest();
    assertEquals("c1=1; c2=2", lastRequest.getAdditionalHeaders().get("Cookie"));
}

From source file:com.gargoylesoftware.htmlunit.CookieManagerTest.java

License:Apache License

/**
 * @throws Exception if the test fails/* w ww .j a v a  2 s  .  co  m*/
 */
@Test
// TODO [IE]SINGLE-VS-BULK test runs when executed as single but breaks as bulk
public void orderCookiesByPath_fromJs() throws Exception {
    final String html = "<html><body><script>\n" + "document.cookie = 'exampleCookie=rootPath;path=/';\n"
            + "document.cookie = 'exampleCookie=currentPath;path=/testpages/';\n" + "</script>\n"
            + "<a href='/testpages/next.html'>next page</a>\n" + "</body></html>";

    getMockWebConnection().setDefaultResponse("");

    final WebDriver webDriver = getWebDriver();
    webDriver.manage().deleteAllCookies();

    loadPage2(html);
    webDriver.findElement(By.linkText("next page")).click();

    final WebRequest lastRequest = getMockWebConnection().getLastWebRequest();
    assertEquals("exampleCookie=currentPath; exampleCookie=rootPath",
            lastRequest.getAdditionalHeaders().get("Cookie"));
}

From source file:com.gargoylesoftware.htmlunit.HistoryTest.java

License:Apache License

/**
 * Tests going in history {@link History#back()} with {@code POST} request.
 *
 * @throws Exception if an error occurs/* ww w  . jav  a2  s  .  c  o m*/
 */
@Test
public void post() throws Exception {
    final Map<String, Class<? extends Servlet>> servlets = new HashMap<>();
    servlets.put("/post1", Post1Servlet.class);
    servlets.put("/post2", Post2Servlet.class);
    servlets.put("/post3", Post3Servlet.class);
    startWebServer("./", new String[0], servlets);

    final WebDriver driver = getWebDriver();
    driver.get(URL_FIRST + "post1");

    driver.findElement(By.id("mySubmit")).click();
    assertEquals(URL_FIRST + "post2", driver.getCurrentUrl());
    assertTrue(driver.getPageSource().contains("POST"));
    assertTrue(driver.getPageSource().contains("para1=value1"));

    driver.findElement(By.linkText("Go to GET")).click();
    assertEquals(URL_FIRST + "post3", driver.getCurrentUrl());
    assertTrue(driver.getPageSource().contains("GET"));

    driver.navigate().back();
    assertEquals(URL_FIRST + "post2", driver.getCurrentUrl());
    assertTrue(driver.getPageSource().contains("POST"));
    assertTrue(driver.getPageSource().contains("para1=value1"));
}

From source file:com.gargoylesoftware.htmlunit.html.HtmlAnchorTest.java

License:Apache License

/**
 * @exception Exception If the test fails
 *//*from www . j a  v  a  2  s  .com*/
@Test
@BuggyWebDriver(IE)
public void shiftClick() throws Exception {
    final String html = "<html><head><title>First</title></head><body>\n" + "<a href='" + URL_SECOND
            + "'>Click Me</a>\n" + "</form></body></html>";

    getMockWebConnection().setResponse(URL_SECOND, "<head><title>Second</title>");
    final WebDriver driver = loadPage2(html);

    final WebElement link = driver.findElement(By.linkText("Click Me"));

    final String originalTitle = driver.getTitle();

    final int windowsSize = driver.getWindowHandles().size();

    new Actions(driver).moveToElement(link).keyDown(Keys.SHIFT).click().keyUp(Keys.SHIFT).perform();

    assertEquals("Should have opened a new window", windowsSize + 1, driver.getWindowHandles().size());
    assertEquals("Should not have navigated away", originalTitle, driver.getTitle());
}

From source file:com.gargoylesoftware.htmlunit.html.HtmlAnchorTest.java

License:Apache License

/**
 * @exception Exception If the test fails
 */// w ww  .j a v a  2 s  . co m
@Test
@BuggyWebDriver({ IE, FF })
public void ctrlClick() throws Exception {
    final String html = "<html><head><title>First</title></head><body>\n" + "<a href='" + URL_SECOND
            + "'>Click Me</a>\n" + "</form></body></html>";

    getMockWebConnection().setResponse(URL_SECOND, "<head><title>Second</title>");
    final WebDriver driver = loadPage2(html);

    final WebElement link = driver.findElement(By.linkText("Click Me"));

    final String originalTitle = driver.getTitle();

    final int windowsSize = driver.getWindowHandles().size();

    new Actions(driver).moveToElement(link).keyDown(Keys.CONTROL).click().keyUp(Keys.CONTROL).perform();

    assertEquals("Should have opened a new window", windowsSize + 1, driver.getWindowHandles().size());
    assertEquals("Should not have navigated away", originalTitle, driver.getTitle());
}

From source file:com.gargoylesoftware.htmlunit.HttpWebConnection3Test.java

License:Apache License

/**
 * @throws Exception if the test fails/* www.  jav  a2 s  .co m*/
 */
@Test
@Alerts(CHROME = { "Host", "Connection", "Accept", "User-Agent", "Referer", "Accept-Encoding",
        "Accept-Language", "Cookie" }, FF = { "Host", "User-Agent", "Accept", "Accept-Language",
                "Accept-Encoding", "Referer", "Cookie", "Connection" }, IE = { "Accept", "Referer",
                        "Accept-Language", "User-Agent", "Accept-Encoding", "Host", "DNT", "Connection",
                        "Cookie" })
@NotYetImplemented(IE)
public void headers_cookie_referer() throws Exception {
    final String htmlResponse = "<a href='2.html'>Click me</a>";
    final String response = "HTTP/1.1 200 OK\r\n" + "Content-Length: " + htmlResponse.length() + "\r\n"
            + "Content-Type: text/html\r\n" + "Set-Cookie: name=value\r\n" + "\r\n" + htmlResponse;

    primitiveWebServer_ = new PrimitiveWebServer(PORT, response.getBytes());
    primitiveWebServer_.start();
    final WebDriver driver = getWebDriver();

    driver.get("http://localhost:" + PORT + "");
    driver.findElement(By.linkText("Click me")).click();

    final Wait<WebDriver> wait = new WebDriverWait(driver, 5);
    wait.until(currentUrlContains("2.html"));

    int index = 1;
    String request;
    do {
        request = primitiveWebServer_.getRequests().get(index++);
    } while (request.contains("/favicon.ico"));

    final String[] headers = request.split("\\r\\n");
    final String[] result = new String[headers.length - 1];
    for (int i = 0; i < result.length; i++) {
        final String header = headers[i + 1];
        result[i] = header.substring(0, header.indexOf(':'));
    }
    assertEquals(Arrays.asList(getExpectedAlerts()).toString(), Arrays.asList(result).toString());
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.css.CSSStyleSheetTest.java

License:Apache License

/**
 * @throws Exception if an error occurs/*from  w  w  w. ja va 2  s  . c  o  m*/
 */
@Test
@Alerts("rgb(255, 0, 0)")
public void veryBig() throws Exception {
    getWebDriver();

    int maxInMemory = 0;
    final WebClient webClient = getWebWindowOf((HtmlUnitDriver) getWebDriver()).getWebClient();
    if (webClient != null) {
        maxInMemory = webClient.getOptions().getMaxInMemory();
    }

    final String html = "<html>\n" + "  <head>\n" + "    <link href='" + URL_FIRST
            + "style.css' rel='stylesheet'></link>\n" + "  </head>\n" + "  <body>\n"
            + "    <a href='second.html'>second page</a>\n" + "  </body>\n" + "</html>";

    final String html2 = "<html>\n" + "  <head>\n" + "    <link href='" + URL_FIRST
            + "style.css' rel='stylesheet'></link>\n" + "  </head>\n" + "  <body class='someRed'>\n"
            + "    <script>\n" + "      var getStyle = function(e) {\n"
            + "        return window.getComputedStyle(e, '');\n" + "      };\n"
            + "      alert(getStyle(document.body).color);\n" + "    </script>\n" + "  </body>\n" + "</html>";

    final MockWebConnection conn = getMockWebConnection();
    final List<NameValuePair> headers2 = new ArrayList<>();
    headers2.add(new NameValuePair("Last-Modified", "Sun, 15 Jul 2007 20:46:27 GMT"));
    final String bigContent = ".someRed { color: red; }" + StringUtils.repeat(' ', maxInMemory);
    conn.setResponse(new URL(URL_FIRST, "style2.css"), bigContent, 200, "OK", "text/css", headers2);
    conn.setResponse(new URL(URL_FIRST, "second.html"), html2);

    final List<NameValuePair> headers1 = new ArrayList<>();
    headers1.add(new NameValuePair("Location", "style2.css"));
    conn.setResponse(new URL(URL_FIRST, "style.css"), "", 302, "Moved", "text/css", headers1);

    final WebDriver driver = loadPage2(html, new URL(URL_FIRST, "test.html"));
    driver.findElement(By.linkText("second page")).click();
    verifyAlerts(driver, getExpectedAlerts());
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.DocumentTest.java

License:Apache License

/**
 * @throws Exception if the test fails/*from www .  ja v  a  2  s  . com*/
 */
@Test
@Alerts("URL")
public void referrer() throws Exception {
    final String firstHtml = "<html><head><title>First</title></head><body>\n" + "<a href='" + URL_SECOND
            + "'>click me</a></body></html>";

    final String secondHtml = "<html><head><title>Second</title></head><body onload='alert(document.referrer);'>\n"
            + "</form></body></html>";
    getMockWebConnection().setResponse(URL_SECOND, secondHtml);

    final WebDriver driver = loadPage2(firstHtml);
    driver.findElement(By.linkText("click me")).click();

    expandExpectedAlertsVariables(URL_FIRST);
    assertEquals(getExpectedAlerts(), getCollectedAlerts(driver));
}