How To Open, Switch, & Work In New Window-Tab
► Watch Complete Selenium 4 Playlist/Series [ Ссылка ]
► Download Code, Presentation, & Transcript From GitHub [ Ссылка ]
► Video 18 Close vs Quit [ Ссылка ]
► Video 71 How To Handle Windows [ Ссылка ]
► Free Selenium PDF Book: [ Ссылка ]
► Free Java PDF Book: [ Ссылка ]
► All Paperback & eBooks: [ Ссылка ]
Social Media Contact
✔ YouTube [ Ссылка ]
✔ Twitter [ Ссылка ]
✔ LinkedIn [ Ссылка ]
✔ GitHub [ Ссылка ]
✔ Facebook [ Ссылка ]
► Transcript
Our steps for opening, switching, working, and switching back to the parent is to access this Home page of this Automation Practice site then open the Sign In page in the new tab. On the Sign In page, we will work in this tab by entering an email address then clicking the Create An Account button. We are not going to perform any actions to create an account but switch back to the parent tab and start shopping.
Let’s start our test by writing @Test / public void testOpenSwitchWorkInNewTab () {} then some comments as our steps. // Open & Switch To New Window-Tab
// Work In The New Window-Tab // Get The Window ID Handles // Switch Back To The Parent Window-Tab & Work In The 1st Window-Tab.
First Step: We are going to Open & Switch To The New Window-Tab, we write driver.switchTo().newWindow(WindowType.TAB).get(“[ Ссылка ]); Paste the URL. Since this URL is so long, we can place the get method on the next line my hitting enter after the dot. You can also end the previous line with a semi-colon and start the next line with driver.get. However, I’m going to remove the semi-colon and driver. It’s still long. But let’s print the title sysout(“Title: ” + driver.getTitle());
Now, let’s work in the new tab. Go back to the AUT and inspect both elements. Email address has email_create as the id value and the button has SubmitCreate as the id value. Capital S and Capital C. Okay, for the email we write driver.findElement(By.id(“email_create”)).sendKeys(“Automation34@Selenium4.com”); then driver.findElement(By.id(“SubmitCreate”)).click(); for the button.
We are finished working in the new tab. At this point, you can decide to close the new tab or leave it open. If you close the tab, make sure to use driver.close and not driver.quit. driver.close will close the new window but driver.quit will quit the driver and close both windows. Video 18 that will show you the difference between close and quit.
Now, let’s get the Window Handles of each tab. The Window Handle is an alphanumeric id assigned to each window or each tab. Set String allWindows = driver.getWindow and we 2 methods. The getWindowHandle method returns 1 window id handle. getWindowHandles method return a set of window handles that can be used to iterate over all open windows. Select this method. The next method is to return an Iterator for the collection of both Window ID’s. String iterate = allWindows.iterator(); We need a way to iterate the collection of elements and next() is a method for returning an element.
String parentFirstWindow = is the parent tab and first element in the iteration. iterate dot is the next() element which is String childSecondWindow = and that window is the new tab we are going to open. For this example, that’s all we need to get the ID’s and iterate the ID’s. You can also loop through the windows.
The last step is to switch back to the parent. Let’s write driver.switchTo().window. Let me explain this part. Do you see window and newWindow? Both methods involve switching focus to a window but the window() method deals with handling a window and the newWindow() method opens a window. The window() method receives a name or handle. We get the name or handle information after using the getWindowHandle method or getWindowHandles method. There’s a difference between handling the window and opening the window. I created video 71 that provide more details on how to handle the windows. In that video, I used all 3 methods: getWindowHandle, getWindowHandles, and driver.switchTo().window().
Right now, we are opening a new window then working in the window which is a benefit because it allows us to multi-task in a different tab. Before Selenium 4, we could not multi-task but had to perform a different task in the same tab. Select window then pass in parentFirstWindow because we are switching back to the parent. Let’s go to the AUT and inspect the Search box.
#Selenium4NewWindowTab #Selenium4NewFeature
Ещё видео!