繁体   English   中英

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

[英]How do I write test to check the browser scrolls down using webdriver, testng?

我有一个链接停留在同一页面上,但在屏幕上向下滚动

如果您访问websiste test.naimi.me并单击此处: kk

该链接应将您向下滚动一点,如何在Selenium Webdriver中测试此操作,我使用testng并用Java编写。

编辑:@伸出援助之手

    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>

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

激活链接后,验证页面的botton元素是否可见。 我对python代码更满意,但在java中,调用应为:

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

isDisplayed()是用于验证网页中是否存在Web元素的方法。 该方法设计为每次成功和失败都会产生一个布尔值。 如果在网页上存在指定的Web元素,则该方法返回“ true”值,如果在网页上不存在该Web元素,则该方法返回“ false”值。

例:

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

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

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

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

}
else
{

  // Write code to skip it
}

要查找链接元素,可以使用Id,xpath,任何类。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM