3

我正在使用 Selenium 来测试我的用户界面。

我正在尝试使用@FindBy-Annotation。以下代码可以正常工作:

@FindBy(how=How.XPATH, xpath ="//input[contains(@id,'idOfInputField')]")
    private WebElement someWebElement;

private void someMethod(){
    WebElement a = someWebElement.findElement(By.xpath("//a[contains(@class, 'ui-spinner-up')][1]"));
    WebElement span1 = a.findElement(By.xpath("//a[contains(@class, 'ui-spinner-up')][1]"));
    WebElement span2 = span1.findElement(By.xpath("//span[contains(@class, 'ui-button-text')][1]"));
    WebElement b = span2.findElement(By.xpath("//span[contains(@class,'ui-icon ui-icon-triangle-1-n')]"));
    b.click();
}

我想使用下面的代码,因为它是基于Annotation的,但它不起作用,虽然我认为它完全一样:

@FindBy(how=How.XPATH, xpath ="//input[contains(@id,'idOfInputField')]"
        + "//a[contains(@class, 'ui-spinner-up')][1]"
        + "//a[contains(@class, 'ui-spinner-up')][1]"
        + "//span[contains(@class, 'ui-button-text')][1]"
        + "//span[contains(@class,'ui-icon ui-icon-triangle-1-n')]")
private WebElement someWebElement;

pivate void someMethod(){
    someWebElement.click();
}

我得到的是以下异常:

  java.lang.RuntimeException: Cannot invoke click on element null. Cannot find it.
    Cause: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//input[contains(@id,'idOfInputField')]//a[contains(@class, 'ui-spinner-up')][1]//span[contains(@class, 'ui-button-text')][1]//span[contains(@class,'ui-icon ui-icon-triangle-1-n')]"}
    Command duration or timeout: 14 milliseconds
    For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
    Build info: version: '2.32.0', revision: '6c40c187d01409a5dc3b7f8251859150c8af0bcb', time: '2013-04-09 10:39:28'
    System info: os.name: 'Linux', os.arch: 'amd64', os.version: '3.5.0-28-generic', java.version: '1.7.0_21'
    Session ID: 92f605a1-0a63-4ba0-b290-ca724b3c6386
    Driver info: org.openqa.selenium.firefox.FirefoxDriver
    Capabilities [{platform=LINUX, databaseEnabled=true, cssSelectorsEnabled=true, javascriptEnabled=true, acceptSslCerts=true, handlesAlerts=true, browserName=firefox, browserConnectionEnabled=true, nativeEvents=false, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=20.0}]
        at path.to.package.utils.StaleReferenceAwareFieldDecorator$StaleReferenceAwareElementLocator.in

    voke(StaleReferenceAwareFieldDecorator.java:86)
            at com.sun.proxy.$Proxy9.click(Unknown Source)
            at 
    ....
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
            at  
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:69)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
        at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
        at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
        at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

有人可以解释这种行为的原因是什么,或者这两段代码之间是否真的存在差异?

4

4 回答 4

3

首先,您不能使用如此复杂的 xpath,这会破坏页面对象的可读性。您的选择器应尽可能简洁。其次,您不应该使用 XPATH 并使用 CSS 选择器。现在回到你的问题,

 @FindBy(css="span[class*='ui-icon-triangle-1-n']")
 WebElement b;

或尝试

 @FindBy(css="span[class*='ui-button-text']>span[class*='ui-icon-triangle-1-n']")
 WebElement b;
于 2013-07-24T01:28:35.890 回答
1

编辑:您在第一个示例中有两个 XPath 选择器//a[contains(@class, 'ui-spinner-up')][1],但在您的第二个示例中只有一个...这可能会导致您的问题。

于 2013-07-23T20:10:13.963 回答
0

PageFactory类通过 .initObjects 方法使用 @FindBy 注解来加载注解。如果我的页面对象通过扩展 LoadableComponent 来实现 LoadableComponent 接口,我会经常使用此方法。(注意:当然,通过扩展 LoadableComponent 你不能通过你的页面对象扩展另一个类,但是如果你在实现的接口中使用静态方法,JDK8 特性,那么你可以获得类似于扩展多个类的功能,或者像特征/混合.)

于 2013-07-23T20:03:08.620 回答
0

页面对象需要装饰对象。我认为您收到此错误是因为您没有装饰所需的类。使用方法 initElements(yourdriverobject, classToBeDecorated)。这应该可以解决问题。如果您需要任何帮助,请告诉我。

于 2013-07-24T05:01:09.980 回答