Selenium fluent等待和页面工厂
所以,我一直在为这个挑战绞尽脑汁,到目前为止我还没有赢。 如果有人能帮助我,我将不胜感激。 详情如下 下面是代码示例:1Selenium fluent等待和页面工厂,selenium,page-factory,Selenium,Page Factory,所以,我一直在为这个挑战绞尽脑汁,到目前为止我还没有赢。 如果有人能帮助我,我将不胜感激。 详情如下 下面是代码示例:1 import org.openqa.selenium.By; import org.openqa.selenium.support.FindBy; import org.openqa.selenium.support.How; public class LocatorTest{ @FindBy(how = How.XPATH, using="//input[@id='id
import org.openqa.selenium.By;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
public class LocatorTest{
@FindBy(how = How.XPATH, using="//input[@id='identifierIdd']")
public WebElement elementTest;
}
1.在示例1中,不正确的xpath是this“//input[@id='identifierIdd']”
2.这是故意的,只是为了等待。正如预期的那样,1分钟后测试退出,异常情况为org.openqa.selenium.NoSuchElementException:
import com.google.common.base.Function;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.SECONDS;
public class Tester01 {
public static void main(String[] args) {
WebDriver webDriver = new ChromeDriver();
webDriver.get("https://accounts.google.com/
signin/v2/identifier?
continue=https%3A%2F%2Fmail.google.com%2Fmail%2F
&service=mail&sacu=1&rip=1&flowName=GlifWebSignIn
&flowEntry=ServiceLogin");
webDriver.manage().window().maximize();
WebElement webElement;
Wait<WebDriver> fluentWaiter = new FluentWait<WebDriver>
(webDriver).withTimeout(1, MINUTES)
.pollingEvery(5, SECONDS)
.withMessage("element couldn't be found after 1 minutes")
.ignoring(NoSuchElementException.class);
webElement = fluentWaiter.until(new Function<WebDriver,
WebElement>()
{
public WebElement apply(WebDriver driver) {
return driver.findElement(By.xpath
("//input[@id='identifierIdd']"));
}
});
webElement.sendKeys("testemail.com");
}
}
和Tester01
class和LocatorTest
class
public class Tester01 {
public static void main(String[] args) {
WebDriver webDriver = new ChromeDriver();
webDriver.get("https://accounts.google.com/signin/v2/identifier?
continue=https%3A%2F%2Fmail.google.
com%2Fmail%2F&service=mail&sacu=1&rip=1
&flowName=GlifWebSignIn&flowEntry=ServiceLogin");
webDriver.manage().window().maximize();
WebElement webElement;
Wait<WebDriver> fluentWaiter = new FluentWait<WebDriver>(webDriver)
.withTimeout(1, MINUTES)
.pollingEvery(5, SECONDS)
.withMessage("element couldn't be found after 1 minutes")
.ignoring(NoSuchElementException.class);
LocatorTest locatorTest = new LocatorTest();
PageFactory.initElements(webDriver, locatorTest);
webElement = fluentWaiter.until
(new Function<WebDriver, WebElement>()
{
public WebElement apply(WebDriver driver) {
return locatorTest.elementTest;
}
});
webElement.sendKeys("testemail.com");
}
}
公共类测试员01{
公共静态void main(字符串[]args){
WebDriver WebDriver=新的ChromeDriver();
webDriver.get(“https://accounts.google.com/signin/v2/identifier?
continue=https%3A%2F%2Fmail.google。
com%2Fmail%2F&service=mail&sacu=1&rip=1
&flowName=GlifWebSignIn&flowEntry=ServiceLogin”);
webDriver.manage().window().maximize();
网络元素;
Wait fluentwater=新建FluentWait(webDriver)
.带超时(1,分钟)
.每(5秒)轮询一次
.withMessage(“1分钟后找不到元素”)
.忽略(NoSuchElementException.class);
LocatorTest LocatorTest=新LocatorTest();
initElements(webDriver、locatorTest);
webElement=fluentwater.until
(新功能()
{
公共WebElement应用(WebDriver){
返回测试。元素测试;
}
});
sendKeys(“testemail.com”);
}
}
因此,带有pagefactory的Tester01
类不会流畅地等待1分钟,测试会立即失败,原因是org.openqa.selenium.NoSuchElementException:
import com.google.common.base.Function;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.SECONDS;
public class Tester01 {
public static void main(String[] args) {
WebDriver webDriver = new ChromeDriver();
webDriver.get("https://accounts.google.com/
signin/v2/identifier?
continue=https%3A%2F%2Fmail.google.com%2Fmail%2F
&service=mail&sacu=1&rip=1&flowName=GlifWebSignIn
&flowEntry=ServiceLogin");
webDriver.manage().window().maximize();
WebElement webElement;
Wait<WebDriver> fluentWaiter = new FluentWait<WebDriver>
(webDriver).withTimeout(1, MINUTES)
.pollingEvery(5, SECONDS)
.withMessage("element couldn't be found after 1 minutes")
.ignoring(NoSuchElementException.class);
webElement = fluentWaiter.until(new Function<WebDriver,
WebElement>()
{
public WebElement apply(WebDriver driver) {
return driver.findElement(By.xpath
("//input[@id='identifierIdd']"));
}
});
webElement.sendKeys("testemail.com");
}
}
我想我知道问题是什么,但不知道如何克服这个问题
这是我对问题的解释,在示例2中,FluentWayer实例中的return语句没有使用适当的驱动程序实例
这就是我所说的returnlocatortest.elementTest因为我认为apply()方法接受一个Webdriver
实例
行返回locatorTest.elementTest代码>不使用驱动程序
实例
我的想法正确吗?
有人能帮我解决这个问题吗?
或者提出一个替代方案
请让我知道,如果以上任何一个没有意义或需要更多的信息
提前谢谢
NiroPageFactory.initElements()
将为所有WebElement
和列表
字段创建代理。还可以设置通过
annotation在FindBy中传递的定位器策略。不执行元素的定位。这是一个懒惰的初始化。将click()、sendkeys()
等命令发送到webelement
时,将完成实际位置
现在在第一种情况下,搜索实际的元素
webElement = fluentWaiter.until(new Function<WebDriver,
WebElement>()
{
public WebElement apply(WebDriver driver) {
return driver.findElement(By.xpath
("//input[@id='identifierIdd']"));
}
});
webElement=fluentwater.until(新函数()
{
公共WebElement应用(WebDriver){
返回driver.findElement(By.xpath
(“//输入[@id='identifierIdd']”);
}
});
在第二种情况下,您只是对一个页面对象进行java调用,它将返回代理对象。不涉及硒业务
webElement = fluentWaiter.until
(new Function<WebDriver, WebElement>()
{
public WebElement apply(WebDriver driver) {
return locatorTest.elementTest;
}
});
webElement=fluentwater.until
(新功能()
{
公共WebElement应用(WebDriver){
返回测试。元素测试;
}
});
但是当运行此代码时-webElement.sendKeys(“testemail.com”)代码>,实际元素位置已完成,但失败。这里没有等待。使用webDriver
实例初始化了PageFactory
看一看可以与FluentWait或专用工具一起使用的类。使用类似于fromExpectedConditions
的方法返回ExpectedCondition
或者,您甚至可以查看定位器工厂
,它在搜索元素时等待特定时间段。和。尽管这是一种过分的做法,因为这是针对Ajax案例的,而不是一个错误的定位器。非常感谢您的快速响应。明白了,我将用expectedConditions进行实验,课堂上还有一些后续问题。您在回答中提到,实际位置发生在click()和sendKeys()f3期间。因此,如果我为elementtobelickable、validityOf等创建util方法,它当然会返回webElement,我可以对返回的webElement执行sendKeys()。这是推荐的吗?或者建议在这些util方法中执行sendKeys()?再次感谢您的帮助加载新页面时,在几乎所有情况下,selenium都会在将控制权返回到下一行代码之前处理元素等的加载。所以,在大多数情况下,您不需要使用等待(尽管一个常见的问题是,UI不稳定,并且单击被叠加元素异常所阻止)。但是,如果由于javascript的动态特性,在加载时存在ajax活动,或者某些元素变得(进入)可见等等,那么有时需要等待。我不建议为waits创建util方法,因为这样会节省使用。这只是需要维护和担心更多的代码。太好了,非常感谢@grasshopper的建议,正如你在最后一条评论中所说的,我正在努力解决一个不稳定的UI,因此等待机制中的调查。我知道这些等待方法所需的维护工作,特别是对于我正在开发的当前产品,这些等待的分类提供了很多可靠性。如果大多数元素需要等待,您应该查看隐式等待。尽管要小心混合隐式和显式,但它们并不能很好地结合在一起。如果必须混合使用,请在使用之前将隐式超时设置为0