到达js命令时,Selenium JAR测试用例停止

时间:2017-03-15 16:40:48

标签: javascript java selenium firefox sharepoint

我使用Selenium JUnit为我们公司制作了一个测试脚本,它在公司网络中的我的帐户中运行良好。但是当其他员工运行脚本时,它会在到达JavaScript部分代码时停止。

我发现每个Sharepoint元素的定位符在不同的帐户中有所不同,例如#ctl00_PlaceHolderMain_nameInput在其他帐户中为#ctl01_PlaceHolderMain_nameInput,这就是为什么使用我的代码无法访问它们的原因。但我该如何解决呢?

我们使用Firefox作为webbrowser,用户需要身份验证才能运行测试用例(HTTPS页面)。

如果有人能帮助我,我将不胜感激。

脚本看起来像这样:

    WebElement NewPage;
    NewPage= wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#mp1_0_2_Anchor")));
    NewPage.click();
    Thread.sleep(5000);
//Error appears after execution of the following lines
    if (driver instanceof JavascriptExecutor) {
        ((JavascriptExecutor)driver).executeScript(""
                + "var tBox= document.querySelector('.ms-dlgFrame').contentDocument.querySelector('#ctl00_PlaceHolderMain_nameInput');"
                + "tBox.setAttribute('value','Testcase 13.1');"
                + "var btn= document.querySelector('.ms-dlgFrame').contentDocument.querySelector('#ctl00_PlaceHolderMain_createButton');"
                + "btn.click();"
                );
    }

以下是iFrame>

的Html代码
<div id="s4-workspace" style="height: 175px; width: 390px; overflow-y: auto;">
    <div id="s4-bodyContainer">
        <div id="s4-titlerow" class="s4-pr s4-notdlg s4-titlerowhidetitle ms-titlerowborder" style="width: 372px;">
            <div class="s4-title s4-lp">
                <div class="s4-title-inner">
                    <table class="s4-titletable" cellspacing="0">
                        <tbody>
                            <tr>
                                <td class="s4-titlelogo">
                                    <a id="ctl00_onetidProjectPropertyTitleGraphic" href="/en/News/" target="_blank">
                                        <img id="ctl00_onetidHeadbnnr2" name="onetidHeadbnnr0" src="/_layouts/images/siteIcon.png" alt="News" style="border-width:0px;">
                                    </a>
                                </td>
                                <td class="s4-titletext">
                                    <h1 name="onetidProjectPropertyTitle">

                                            <a id="ctl00_PlaceHolderSiteName_onetidProjectPropertyTitle" href="/sv/News/" target="_blank">News</a>

                                    </h1>
                                    <span id="onetidPageTitleSeparator" class="s4-nothome s4-bcsep s4-titlesep"><span><span style="height:11px;width:11px;position:relative;display:inline-block;overflow:hidden;"><img src="/_layouts/images/fgimg.png" alt=":" style="border-width:0px;position:absolute;left:-0px !important;top:-585px !important;"></span></span> </span>
                                    <h2>

New Page

                                    </h2>
                                    <div class="s4-pagedescription" tabindex="0">

                                    </div>
                                </td>
                                <td class="s4-socialdata-notif">

                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
            <div id="s4-topheader2" class="s4-pr s4-notdlg">
                <a name="startNavigation"></a>
                <div id="s4-searcharea" class="s4-search s4-rp">

阅读this后,我可以捕获下面的错误日志:

  

块引用   JUnit版本4.12   .E   时间:44,583   有1次失败:   1)testId1331(ID131)   org.openqa.selenium.JavascriptException:TypeError:tBox为null   建立信息:版本:&#39;未知&#39;,修订版:&#39; 1969d75&#39;,时间:&#39; 2016-10-18 09:43:45 -0700&#39;   系统信息:主机:&#39; AOS391&#39;,ip:&#39; xxx&#39;,os.name:&#39; Windows 7&#39;,os.arch:&#39; amd64&#39 ;,os.version:&#39; 6.1&#39;,java.version:&#39; 1.8.0_111&#39;   驱动程序信息:org.openqa.selenium.firefox.FirefoxDriver   功能[{rotating = false,raisesAccessibilityExceptions = false,marionette = true,firefoxOptions = {args = [],prefs = {}},appBuildId = 20170125094131,version =,platform = XP,proxy = {},command_id = 1,specificationLevel = 0,acceptSslCerts = false,processId = 14564,browserVersion = 51.0.1,platformVersion = 6.1,XULappId = {ec8030f7-c20a-464f-9b0e-13a3a9e97384},browserName = firefox,takesScreenshot = true,takesElementScreenshot = true,platformName = windows_nt }]   会议ID:324f9226-d3a0-4285-9128-c899ddc6bca6       at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)       at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)       at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)       at java.lang.reflect.Constructor.newInstance(Constructor.java:423)       at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:127)       at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:93)       在org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:42)       在org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:163)       在org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)       在org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)       在org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:537)       在ID131.testId1331(ID131.java:63)       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)       at java.lang.reflect.Method.invoke(Method.java:498)       在org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:50)       在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)       在org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)       在org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)       在org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)       在org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)       在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)       在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)       在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)       在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)       在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)       在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)       在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)       在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)       在org.junit.runners.ParentRunner.run(ParentRunner.java:363)       在org.junit.runners.Suite.runChild(Suite.java:128)       在org.junit.runners.Suite.runChild(Suite.java:27)       在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)       在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)       在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)       在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)       在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)       在org.junit.runners.ParentRunner.run(ParentRunner.java:363)       在org.junit.runner.JUnitCore.run(JUnitCore.java:137)       在org.junit.runner.JUnitCore.run(JUnitCore.java:115)       在org.junit.runner.JUnitCore.runMain(JUnitCore.java:77)       在org.junit.runner.JUnitCore.main(JUnitCore.java:36)       在ID131.main(ID131.java:143)

FAILURES !!! 测试运行:1,失败:1

1 个答案:

答案 0 :(得分:0)

目前尚不清楚为什么使用Javascript代替使用Selenium api的等效Java。如果您尝试这样做会发生什么:

void loop() {
    int serialIndex = 0;  
    int i2cIndex = 0; 
    while (0 < Serial.available()) { // loop through all the received bytes 
    char bufferByte = 0;     
    bufferByte = Serial.read();
    serialBuffer[serialIndex] = (byte) bufferByte;  // put current index byte in array      
    serialIndex ++;                    // add index. 
    if (serialIndex%12==0 && serialIndex != 0) {
          sendBytes(0);      
       }
    } 
     //sendBytes(0);  
     delay(50);
}

void sendBytes(int slave) {
    byte i2cBuffer[12];
    int bufferIndex = slave * 12;
    for (int i = 0; i < 12; i++) {
        i2cBuffer[i] = serialBuffer[i + bufferIndex];
    }
    Wire.beginTransmission(slave+1);
    Wire.write(i2cBuffer, 12);
    Wire.endTransmission();  
}