0
votes

I made a test script for our company using Selenium JUnit, which works fine in my account in the company's network. But when other employees run the script it stops when it reaches JavaScript part of the code.

I found out locators for each Sharepoint element are different in different accounts,like #ctl00_PlaceHolderMain_nameInput is #ctl01_PlaceHolderMain_nameInput in other account, and that's why they are not reachable using my code. But how can I fix it?

We use Firefox as webbrowser and users need authentication to run the test case (HTTPS page).

I would be appreciated if anyone could help me with this.

The Script looks like this:

    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();"
                );
    }

Here is the Html code for iFrame>

<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">

After reading this, I could capture the error log below:

Blockquote JUnit version 4.12 .E Time: 44,583 There was 1 failure: 1) testId1331(ID131) org.openqa.selenium.JavascriptException: TypeError: tBox is null Build info: version: 'unknown', revision: '1969d75', time: '2016-10-18 09:43:45 -0700' System info: host: 'AOS391', ip: 'xxx', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_111' Driver info: org.openqa.selenium.firefox.FirefoxDriver Capabilities [{rotatable=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}] Session 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) at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:42) at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:163) at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601) at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:537) at 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) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 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.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runners.Suite.runChild(Suite.java:128) at org.junit.runners.Suite.runChild(Suite.java:27) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at org.junit.runner.JUnitCore.run(JUnitCore.java:115) at org.junit.runner.JUnitCore.runMain(JUnitCore.java:77) at org.junit.runner.JUnitCore.main(JUnitCore.java:36) at ID131.main(ID131.java:143)

FAILURES!!! Tests run: 1, Failures: 1

1
Can you please post the error appeared. - vijayraj34
We can provide better assistance if you share your code and any messages that appear when it stops. - Mark Lapierre
@vijayraj34 Thank you for your reply I have updated the post with error log and code. - AlanD
@MarkLapierre Thank you for your reply I have updated the post with error log and code. - AlanD
The error suggests the input field doesn't exist, or at least can't be found using the locators specified. Have you tried inspecting the DOM at the point the error occurs? Could you include the HTML as it appears when the error occurs? - Mark Lapierre

1 Answers

0
votes

It's not clear why you're using Javascript instead of the equivalent Java using the Selenium api. What happens if you try this:

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

WebElement tBox = driver.findElement(By.cssSelector(".ms-dlgFrame #ctl00_PlaceHolderMain_nameInput"));
tBox.clear();
tBox.sendKeys("Testcase 13.1");
WebElement btn = driver.findElement(By.cssSelector(".ms-dlgFrame #ctl00_PlaceHolderMain_createButton"));
btn.click();