Below is the code that I wrote where i am using windows handle to validate the url new window being opened after clicking on a link into new page.
package pages;
import java.io.IOException;
import org.openqa.selenium.By;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
import appSetup_Maven.test.BaseClass;
public class HelpCenter extends BaseClass {
@Test
public void Kb() throws IOException {
Login();
driver.findElement(By.xpath(".//*[@id='wrapper']/div[1]/div[2]/div/div/ul/li[3]/a")).click();
Object[] handle = driver.getWindowHandles().toArray();
driver.findElement(By.xpath(".//*[@id='wrapper']/div[1]/div[2]/div/div/ul/li[3]/ul/li[4]/a")).click();
driver.switchTo().window((String) handle[0]);
String URL = driver.getCurrentUrl();
System.out.println(URL);
Assert.assertEquals(URL, "https://preprod.xyz.com/home");
driver.switchTo().window((String) handle[1]);
driver.getTitle();
Assert.assertEquals(URL, "https://kb.xyz.com/");
}
}
However the above code is not working at it fails and gives the below error -
java.lang.ArrayIndexOutOfBoundsException: 1
at pages.HelpCenter.Kb(HelpCenter.java:25)
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.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:661)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:744)
at org.testng.TestRunner.run(TestRunner.java:602)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
at org.testng.SuiteRunner.run(SuiteRunner.java:289)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
at org.testng.TestNG.runSuites(TestNG.java:1144)
at org.testng.TestNG.run(TestNG.java:1115)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
As a Test Engineer, my first point is getting to know the meaning of error and caused it. It can be easily debugged using the traceback that the IDE provides. You should refer to the documentation, and then try to apply logic why it failed.
Now, if I see your code, the exception that comes it ArrayIndexOutOfBoundsException
. As per the documentation from Oracle, this is what they say about ArrayIndexOutOfBoundsException
Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.
And, then your traceback, suggests, this is at line 25
at pages.HelpCenter.Kb(HelpCenter.java:25)
which indicates to this piece of code
driver.switchTo().window((String) handle[1]);
The array that you created doesn't has element at index 1, and so it throws this error.
The reasons may include -
You're not giving enough time for WebDriver to recognise the window. I would suggest adding a Wait
after you click on the web element, before switching to the window.
Using an Object
array to get the windows. You are getting an array of Objects, and then converting this back to String? Why is that? Unless there is an need to be converted to Objects, I don't think you need an array of Objects. You can simply use String
for the window handles itself. Try to simplify your code.
You can use something like
String parentWindowHandler = driver.getWindowHandle();
String subWindowHandler = null;
Set<String> handles = driver.getWindowHandles();
Iterator<String> iterator = handles.iterator();
while (iterator.hasNext())
{
subWindowHandler = iterator.next();
driver.switchTo().window(subWindowHandler);
System.out.println(subWindowHandler);
}
driver.switchTo().window(parentWindowHandler);
Here is an excellent post on IndexOutOfBoundsException.