selenium, tutorial,

Working with link text, hyperlink text

Ha Do Ha Do Follow Jan 27, 2019 · 1 min read
Working with link text, hyperlink text
Share this

Link Text hay Hyperlink là đối tượng mà khi ngừoi dùng click trên nó có thể chuyển tiếp một trang web khác.

Đối tượng này có một attributehref=''. Selenium cho chúng ta một method để làm việc với dạng đối tượng này thông qua text bằng By.linkText().

Test case

Steps

  • Open browser
  • Navigate to https://the-internet.herokuapp.com/status_codes
  • Click on “200”
  • Then “200 status code” page appear
  • Click on “go here”
  • Click on “301”
  • Then “301 status code” page appear
  • Click on “go here”
  • Click on “404”
  • Then “404 status code” page appear
  • Click on “go here”
  • Click on “500”
  • Then “500 status code” page appear
  • Click on “go here”

Test case as code

 @Test
    void hyperlink(){
        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();
        driver.get("https://the-internet.herokuapp.com/status_codes");

        driver.findElement(By.linkText("200"));
        Assert.assertEquals(driver.getCurrentUrl(),"https://the-internet.herokuapp.com/status_codes/200");
        driver.findElement(By.linkText("here"));

        driver.findElement(By.linkText("301"));
        Assert.assertEquals(driver.getCurrentUrl(),"https://the-internet.herokuapp.com/status_codes/301");
        driver.findElement(By.linkText("here"));

        driver.findElement(By.linkText("404"));
        Assert.assertEquals(driver.getCurrentUrl(),"https://the-internet.herokuapp.com/status_codes/404");
        driver.findElement(By.linkText("here"));

        driver.findElement(By.linkText("500"));
        Assert.assertEquals(driver.getCurrentUrl(),"https://the-internet.herokuapp.com/status_codes/500");
        driver.findElement(By.linkText("here"));

        driver.findElement(By.linkText("here"));
        Assert.assertEquals(driver.getCurrentUrl(),"https://the-internet.herokuapp.com/status_codes");
    }
Ha Do
Written by Ha Do Follow
Hi, I am Ha, the author of Tvn education center site