While working on a test script for our company using Selenium JUnit, I encountered an issue. The script runs smoothly in my account on the company's network but encounters a roadblock when other employees try to run it, specifically when it reaches the JavaScript segment.
I realized that the locators for each Sharepoint element vary across different accounts. For example, #ctl00_PlaceHolderMain_nameInput
in my account is actually #ctl01_PlaceHolderMain_nameInput
in another account, making these elements unreachable through my code. How can this issue be resolved?
Our web browser of choice is Firefox, and users are required to authenticate to run the test case (HTTPS page).
Any assistance in resolving this matter would be greatly appreciated.
The snippet of the script causing trouble looks like this:
WebElement NewPage;
NewPage = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#mp1_0_2_Anchor")));
NewPage.click();
Thread.sleep(5000);
// Error occurs after executing 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 the 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">
Upon referring to this resource, I was able to extract the error log below:
Error Log: 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... Session ID: ... at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) (Further Stack Trace details omitted for brevity)
FAILURE! Tests run: 1, Failures: 1