org.openqa.selenium.nosuchelementexception确认元素何时存在

时间:2015-04-21 08:35:31

标签: java unit-testing selenium extjs junit

我有一个简单的Extjs-5应用程序,我正在编写测试。 应用程序在我的本地服务器上运行良好。我有一个搜索字段,我可以通过Selenium和JUnit找到并测试,但是当我尝试点击我的应用程序中的按钮时,我得到以下异常

    org.openqa.selenium.NoSuchElementException: no such element
  (Session info: chrome=42.0.2311.90)
  (Driver info: chromedriver=2.9.248315,platform=Windows NT 6.1 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 31 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.33.0', revision: '4e90c97', time: '2013-05-22 15:32:56'
System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_75'
Session ID: b2d6caa7ef4ee5c396c65899baa2b8a5
Driver info: org.openqa.selenium.remote.RemoteWebDriver
Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, browserName=chrome, chrome={userDataDir=C:\Users\SUESSD~1\AppData\Local\Temp\scoped_dir6284_5598}, rotatable=false, locationContextEnabled=true, version=42.0.2311.90, takesHeapSnapshot=true, cssSelectorsEnabled=true, databaseEnabled=false, handlesAlerts=true, browserConnectionEnabled=false, webStorageEnabled=true, nativeEvents=true, applicationCacheEnabled=false, takesScreenshot=true}]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:191)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:307)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:404)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:344)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:299)
    at com.appfoundation.automation.framework.BaseModel.find(BaseModel.java:28)
    at com.appfoundation.automation.example.model.MyModel.findAddButton(MyModel.java:30)
    at com.appfoundation.automation.example.MyBaseTest.testResetForm(MyBaseTest.java:32)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    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:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

我的测试课程是:

public class MyBaseTest extends BaseSeleniumTest {

    private MyModel model;

    public MyBaseTest() {

        model = new MyModel(this);
    }

    @Test
    public void testResetForm(){

        driver.get("http://localhost:1841/");

        this.waitToBeClickable(MyModel.SEARCH_XPATH);

        WebElement searchInput = model.findSearchInput();
        WebElement addButton  = model.findAddButton();

        searchInput.sendKeys("Yawar");
        assertEquals("Yawar", searchInput.getAttribute("value"));
        addButton.click(); // Test Breaking at this line with the above mentioned exception 
    }
}

这是我的模特课:

public class MyModel extends BaseModel {

    public static final String SEARCH_XPATH = XPathUtils.findInputByName("searchField");
    public static final String ADD_XPATH = XPathUtils.findButtonByText("Add");

    public MyModel(BaseSeleniumTest test) {
        super(test);        
    }       
    public WebElement findSearchInput(){            
        return this.find(SEARCH_XPATH);
    }       
    public WebElement findAddButton(){

        System.out.println(ADD_XPATH);
        return find(ADD_XPATH);
    }   
}

我在堆栈溢出方面发现了类似的问题,例如org.openqa.selenium.NoSuchElementException: no such element 但他们没有接受答案,他们建议增加我做的等待,但在任何一种情况下都会发生同样的异常。 请帮助。

0 个答案:

没有答案