如何使用webdriver,testng编写测试以检查浏览器向下滚动?

时间:2015-02-19 07:55:48

标签: java selenium webdriver testng

我有一个链接保持在同一页面但向下滚动

如果你去websiste test.naimi.me并点击这里: kk

链接应该向下滚动一下,如何在selenium webdriver中测试这个动作,我使用testng并在java中编写。

编辑:@Helping hands

    package erjan.test.naimi.me;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.Assert;
public class SpecialistLoads {
    WebDriver firefox ;
  @Test
  public void main() {
      //By.xpath("//a[@href=\"/astana/\"]/img") )) ;
      WebElement all_specs = firefox.findElement(By.xpath("//a[@href=\"/astana/#specialists\"]")) ;



      all_specs.click();
      String all_specalists_url = firefox.getCurrentUrl();
      Assert.assertEquals(all_specs, all_specialists_url );
  }
  @BeforeMethod
  public void beforeMethod() {
      firefox = new  FirefoxDriver() ;
      firefox.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      //Launch the website
      firefox.get("http://test.naimi.me");
  }

  @AfterMethod
  public void afterMethod() {
      firefox.quit();

  }

}

红色箭头指向的链接有html代码:

a data-toggle="menu" href="/astana/#specialists">Все специалисты</a>

3 个答案:

答案 0 :(得分:1)

为了测试它,我会使用间接检查。

验证激活链接后是否可以看到页面的botton元素。我对python代码感觉更舒服但在java中调用应该是:

WebElement.isDisplayed()检查元素是否可见

  

isDisplayed()是用于验证Web元素是否存在的方法   在网页内。该方法旨在产生布尔值   每一次成功和失败。如果,该方法返回“true”值   指定的Web元素出现在网页上并显示“false”   如果网页上没有网页元素,则为值。

示例:

boolean footer_loaded=driver.findElement(By.id(“footer”)).isDisplayed();

答案 1 :(得分:1)

如果在页面加载时链接不可见,那么您可以使用 isdisplayed 方法,如下所示:

WebElement link = driver.findElement(By.xpath(yourlinkxpath"));

If(link.isDisplayed())
{
   // Write code to click on link

}
else
{

  // Write code to skip it
}

要查找link元素,您可以使用Id,xpath,class any。

答案 2 :(得分:0)

您可以Element.getBoundingClientRect()检查元素的大小及其相对于视口的位置。您需要使用driver.executeScript()。