我为什么要用@FindByvs driver.findElement()?
@FindBy强迫我将所有变量移动到类级别(当大多数变量只需要在方法级别时).它似乎唯一能给我买的是我可以调用PageFactory.initElements(),它为我处理延迟初始化.
我错过了什么?
我正在尝试学习PageFactory模型.我理解这样一个事实,当我们这样做时initElements,WebElements就位于其中.比如说,我点击了一个webelement,因此DOM中的其他一个元素发生了变化.现在,显然我会在StaleElementReferenceException这里得到一个.我该如何解决这个问题?
我是否应该再次发现特定的WebElement知道DOM中的WebElement属性可能发生了变化?还是有另一种方法来处理这个问题?
selenium pageobjects selenium-webdriver page-factory staleelementreferenceexception
我正在用c#创建一个测试自动化框架.在项目中,我有"的顶部使用OpenQA.Selenium.Support.PageObjects;",但是当我尝试引用PageFactory.initElements(驱动程序,页面名称),我看到消息"名'PageFactory’不存在中目前的背景".
我认为PageFactory类是作为Selenium.Support nu get包的一部分包含的.在线教程似乎以与我没有额外导入相同的方式引用PageFactory,有什么我缺少的吗?
NuGet包:
代码如下:
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.PageObjects;
using System;
using System.IO;
namespace Framework.TestCases
{
class TestCase
{
[Test]
public void Test()
{
String pathToDrivers = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName + "\\Drivers";
IWebDriver driver = new ChromeDriver(pathToDrivers);
String searchTerm = "Search Term";
driver.Url = "https://website.com/";
driver.Manage().Window.Maximize();
HomePage homePage = new HomePage(driver);
PageFactory.initElements(driver, homePage);
}
}
}
Run Code Online (Sandbox Code Playgroud) 我在运行 Appium 测试时遇到此异常。PageFactory 上的测试失败,但出现以下异常。
\nMy POM:\n<?xml version="1.0" encoding="UTF-8"?>\n<project xmlns="http://maven.apache.org/POM/4.0.0`\n xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">\n <modelVersion>4.0.0</modelVersion>\n\n <groupId>com.telepathy.test</groupId>\n <artifactId>twitter</artifactId>\n <version>1.0-SNAPSHOT</version>\n <dependencies>\n <dependency>\n <groupId>junit</groupId>\n <artifactId>junit</artifactId>\n <version>4.12</version>\n </dependency>\n <!-- https://mvnrepository.com/artifact/io.appium/java-client -->\n <dependency>\n <groupId>io.appium</groupId>\n <artifactId>java-client</artifactId>\n <version>7.2.0</version>\n </dependency>\n </dependencies>\n\n</project>\n\nRun Code Online (Sandbox Code Playgroud)\n此处失败:\nPageFactory.initElements(new AppiumFieldDecorator(this.driver), this);
\njava.lang.RuntimeException: java.lang.NoSuchMethodException: \n jdk.proxy2.$Proxy9.proxyClassLookup()\n at \xe2\x80\x8bio.appium.java_client.pagefactory.bys.builder.AppiumByBuilder.prepareAnnotationMethods(\nApiumByBuilder.java:84)\nRun Code Online (Sandbox Code Playgroud)\n 我正在使用Selenium pagefactory,并引入了一个注释@Name(Description =“ Username”),我将其用于所有WebElement。稍后,我需要在自定义方法中找到Description的值,以进行报告,例如:
public static void click(WebElement element) throws Throwable {
try {
element.click();
} catch(ElementNotInteractableException E1) {
throw new UnableToInteractWithElementException("Unable To Interact With " + WebElement Descriptionn);
}
}
Run Code Online (Sandbox Code Playgroud)
我的@Name批注界面和pagefactory如下所示:
名称界面
@Target({ElementType.FIELD, ElementType.TYPE,ElementType.CONSTRUCTOR})
public @interface Name {
String Description() default "";
}
Run Code Online (Sandbox Code Playgroud)
@Name(Description = "Username")
@FindBy(id="txtLoginID")
public WebElement username;
Run Code Online (Sandbox Code Playgroud)
我在使用反射时遇到的问题是需要定义pagefactory的类名,还需要提供字段名作为字符串“ username”来检索注释值。
我希望能够仅通过提供我的WebElement而不向click(WebElement element)方法提供其他对象来检索注释值。
有没有办法使用 PageFactory 注释等待 Selenium 中不存在的元素?
使用时:
@FindBy(css= '#loading-content')
WebElement pleaseWait;
Run Code Online (Sandbox Code Playgroud)
定位元素,然后:
wait.until(ExpectedConditions.invisibilityOfElementLocated(pleaseWait));
Run Code Online (Sandbox Code Playgroud)
我会得到:
org.opeqa.selenium.WebElement cannot be converted to org.openqa.selenium.By
Run Code Online (Sandbox Code Playgroud)
我可以使用以下方法完成我需要的操作:
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector('loading-content')));
Run Code Online (Sandbox Code Playgroud)
但是,我希望能够使用 PageFactory 注释以保持框架一致。有没有办法做到这一点?
通过阅读SeleniumHQ的pageobject文档,他们指定了从不导航到其他页面的方法返回“ this”的示例。我的问题是为什么?
我以为页面对象的状态可能是一个原因,但是页面本身(实际的UI页面)可能会更改状态或刷新,但页面对象本身不会。Page Factory及其@FindBy批注已经确保了每次调用WebElement时都会找到它,因此在这种情况下元素的状态似乎无关紧要。
page-factory ×7
selenium ×6
pageobjects ×3
java ×2
annotations ×1
appium ×1
automation ×1
c# ×1
findby ×1
pom.xml ×1
reflection ×1