Welcome to the Question and Answer site for Software Testing Professionals

You can get your queries resolved from Software Testing Professionals.

Ask a Question

Get answers and give answers to questions from other members.

View All Questions

Get votes and earn points to build your reputation in community.

View Hot Questions

0
Write a code to get the text available in 3rd window and move the control to 2nd window and close it.

Open 1 Answers 111 Views
0 1
This question was asked to me in an interview. I am not able to move to second window and close it. Please help me.
1Comments
commented Sep 30, 2017 by SunilBhaskar (445 points)
can you please share your code. So we know what have you already tried? Also, provide any error stack trace.

1 Answer

0
0
I can help you with generic answer here.

1.) Using driver.getWindowHandles() and store all Window Handles to List<String> variable.

2.) Store current window handle using driver.getCurrentWindowHandle(). So, you are sure you do not switch to the current window during iterating.

3.) Store IInd window title into any string variable say window2title.

4.) Store IIIrd window title to String variable say window3title.

Now you can loop through as given below.

public void sample_psudo_swith_code(){

String currentWindowHandle = driver.getCurrentWindowHandle();

String window2title = "your  title here";

String window3title = "your title here";

switch_to_window(currentWindowHandle,window3title );

//... do your work here with window 3

switch_to_window(currentWindowHandle,window2title );

driver.close(); //switching closing IInd window.

switch_to_window(currentWindowHandle );

}

public void switch_to_window(String windowHandle){

driver.switchTo().window(windowHandle);

}

public void switch_to_window(String currentWindowHandle, String title){

List<String> handles = driver.getWindowHandles();

while(String handle : handles){

if(!handle.equals(currentWindowHandle)){

driver.switchTo().window(handle);

if(driver.getTitle().equals(title)) return;

}

}

}
answered Sep 30, 2017 by SunilBhaskar (445 points)

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.

We have resolved 1.2k+ Software Testing queries!

Software Testing Board Weekly update

We will send you handpicked questions that you would love to answer.

View previous campaigns.

Top Contributors

...