Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium 如何在PageFactory中使用FluentWait_Selenium_Selenium Webdriver - Fatal编程技术网

Selenium 如何在PageFactory中使用FluentWait

Selenium 如何在PageFactory中使用FluentWait,selenium,selenium-webdriver,Selenium,Selenium Webdriver,隐式和显式等待不起作用。 如何修复它 如何在PageFactory中使用FluentWait,以便在测试中不需要使用定位器。 完全不要使用Thread.sleep。 使用的工具:Selenium、TestNG、WebDriverManager 网站是在AngularJS上建立的 public class LoginPage { private WebDriver driver; public StudioMenuPage(WebDriver driver) {

隐式和显式等待不起作用。
如何修复它
如何在PageFactory中使用FluentWait,以便在测试中不需要使用定位器。
完全不要使用Thread.sleep。

使用的工具:Selenium、TestNG、WebDriverManager
网站是在AngularJS上建立的

public class LoginPage {

    private WebDriver driver;

    public StudioMenuPage(WebDriver driver) {
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }

    @FindBy(xpath = "//div[@class='login']")
    private WebElement loginButton;

    public WebElement getLoginButton() {
        return loginButton;
    }
}


public class TestBase {
public static WebDriver driver = null;

@BeforeTest()
public void initialize() {
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
options.addArguments("--no-sandbox");
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    }
}


public class LoginTest extends TestBase {

LoginPage loginPage;    

@Test
private void makeLogin() {

loginPage = new LoginPage(driver);

// Does not work with Implicit Wait:
/* 
loginPage.getLoginButton().click;
*/


// Works with Thread.sleep:
/* 
Thread.sleep(4000);
loginPage.getLoginButton().click;
*/

// Does not work with Explicit Wait:
/* 
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(loginPage.getLoginButton()));
loginPage.getLoginButton().click;
*/

// Works with FluentWait:
/*  
new FluentWait<>(driver).withTimeout(Duration.ofSeconds(5)).pollingEvery(Duration.ofMillis(500))
    .ignoring(WebDriverException.class)
    .until(d -> {
                    WebElement el = d.findElement(By.xpath("//div[@class='login']"));
                    el.click();
                    return el;
                });
*/
}
公共类登录页{
私有网络驱动程序;
公共学习菜单页(WebDriver){
this.driver=driver;
PageFactory.initElements(驱动程序,this);
}
@FindBy(xpath=“//div[@class='login']”)
私有WebElement登录按钮;
公共Web元素getLoginButton(){
返回登录按钮;
}
}
公共类测试库{
公共静态WebDriver驱动程序=null;
@测试前()
公共无效初始化(){
ChromeOptions选项=新的ChromeOptions();
选项。添加参数(“--headless”);
options.addArguments(“--no sandbox”);
WebDriverManager.chromedriver().setup();
驱动程序=新的色度驱动程序(可选);
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
}
}
公共类LoginTest扩展了TestBase{
登录页面登录页面;
@试验
私有void makeLogin(){
登录页面=新登录页面(驱动程序);
//不适用于隐式等待:
/* 
loginPage.getLoginButton()。单击;
*/
//与Thread.sleep一起使用:
/* 
睡眠(4000);
loginPage.getLoginButton()。单击;
*/
//不适用于显式等待:
/* 
WebDriverWait wait=新的WebDriverWait(驱动程序,10);
wait.until(ExpectedConditions.elementtobelickable(loginPage.getLoginButton());
loginPage.getLoginButton()。单击;
*/
//与FluentWait配合使用:
/*  
新建FluentWait(驱动程序)。带超时(持续时间秒(5))。轮询间隔(持续时间毫秒(500))
.忽略(WebDriverException.class)
.直到(d->{
WebElement el=d.findElement(By.xpath(//div[@class='login']);
el.click();
返回el;
});
*/
}
如果使用隐式和显式等待器,则会发生以下错误:

org.openqa.selenium.WebDriverException: unknown error: Element <div class="login">...</div> is not clickable at point (225, 334). Other element would receive the click: <div id="cdk-overlay-0" class="cdk-overlay-pane" dir="ltr" style="pointer-events: auto; top: 316px; left: 201.5px;">...</div>
  (Session info: headless chrome=73.0.3683.86)
  (Driver info: chromedriver=2.46.628411 (3324f4c8be9ff2f70a05a30ebc72ffb013e1a71e),platform=Mac OS X 10.14.4 x86_64) (WARNING: The server did not provide any stacktrace information)
org.openqa.selenium.WebDriverException:未知错误:元素…在点(225334)处不可单击。其他元素将收到单击:。。。
(会话信息:无头镀铬=73.0.3683.86)
(驱动程序信息:chromedriver=2.46.628411(3324f4c8be9ff2f70a05a30ebc72ffb013e1a71e),平台=Mac OS X 10.14.4 x8664)(警告:服务器未提供任何堆栈跟踪信息)

首先:我们不应该混合隐式和显式等待!这样做可能会导致不可预测的等待时间

建议您将PageFactoryAjaxElementLocatorFactory一起使用,它将在任何时候访问每个元素时等待指定的秒数,并忽略缓存查找标记

PageFactory.initElements(新的AjaxElementLocatorFactory(驱动程序,15),此);

如果在给定的时间间隔内找不到元素,它将抛出NoTouchElementException异常

下面的示例显示如何在页面工厂中实现FluentWait-

protected synchronized void waitForelementVisibility并单击(WebElement元素,int超时,String元素名称){
受保护的静态等待=null;
试一试{
wait=新的FluentWait((WebDriver)驱动程序)。带有超时(超时,时间单位。秒)。轮询间隔(1,
时间单位(秒);
等待直到(元素的预期条件可见度);
元素。单击();
}捕获(例外e){
}
}

如果您阅读了错误消息,它会准确地告诉您发生了什么。您试图单击
,但是
遇到了阻碍。您需要首先处理覆盖。如果没有URL或更多描述,我们无法告诉您如何针对您的特定情况执行此操作……可能需要等待覆盖显示、不可见和不可见然后单击您的元素,或者它可能会处理出现的弹出窗口(关闭它,等等)。你需要做更多的研究,然后回来编辑你的问题和其他细节。哦…声称
隐式和显式等待不起作用是有点愚蠢的。其他人都可以很好地使用它们,所以它们是有效的,你只是使用不正确而已。
protected synchronized void waitForElementVisibilityAndClick(WebElement element, int timeOut, String elementName) {
    protected static Wait<WebDriver> wait = null;
    try {
        wait = new FluentWait<WebDriver>((WebDriver) driver).withTimeout(timeOut, TimeUnit.SECONDS).pollingEvery(1,
                TimeUnit.SECONDS);
        wait.until(ExpectedConditions.visibilityOf(element));
        element.click();
    }catch(Exception e) {
    }
}