Using newWindow() for opening a new blank window or blank tab

Selenium 4: New Features Selenium 4 - New Featues
22 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$49.99
List Price:  $69.99
You save:  $20
€46.38
List Price:  €64.94
You save:  €18.55
£39.69
List Price:  £55.57
You save:  £15.88
CA$68.04
List Price:  CA$95.27
You save:  CA$27.22
A$77.05
List Price:  A$107.88
You save:  A$30.82
S$67.53
List Price:  S$94.55
You save:  S$27.01
HK$391.10
List Price:  HK$547.57
You save:  HK$156.47
CHF 45.30
List Price:  CHF 63.42
You save:  CHF 18.12
NOK kr542.56
List Price:  NOK kr759.63
You save:  NOK kr217.06
DKK kr345.97
List Price:  DKK kr484.38
You save:  DKK kr138.41
NZ$83.89
List Price:  NZ$117.46
You save:  NZ$33.56
د.إ183.57
List Price:  د.إ257.02
You save:  د.إ73.44
৳5,487.49
List Price:  ৳7,682.93
You save:  ৳2,195.43
₹4,169.34
List Price:  ₹5,837.41
You save:  ₹1,668.07
RM236.60
List Price:  RM331.26
You save:  RM94.66
₦70,914.31
List Price:  ₦99,285.71
You save:  ₦28,371.40
₨13,903.63
List Price:  ₨19,466.20
You save:  ₨5,562.56
฿1,823.66
List Price:  ฿2,553.28
You save:  ฿729.61
₺1,615.90
List Price:  ₺2,262.39
You save:  ₺646.49
B$249.58
List Price:  B$349.43
You save:  B$99.85
R954.17
List Price:  R1,335.92
You save:  R381.74
Лв90.31
List Price:  Лв126.45
You save:  Лв36.13
₩67,592.09
List Price:  ₩94,634.34
You save:  ₩27,042.24
₪184.30
List Price:  ₪258.03
You save:  ₪73.73
₱2,816.67
List Price:  ₱3,943.57
You save:  ₱1,126.89
¥7,571.47
List Price:  ¥10,600.67
You save:  ¥3,029.19
MX$829.61
List Price:  MX$1,161.53
You save:  MX$331.91
QR182.13
List Price:  QR255
You save:  QR72.86
P685.38
List Price:  P959.59
You save:  P274.21
KSh6,573.68
List Price:  KSh9,203.68
You save:  KSh2,630
E£2,364.52
List Price:  E£3,310.52
You save:  E£946
ብር2,840.03
List Price:  ብር3,976.27
You save:  ብር1,136.24
Kz41,802.18
List Price:  Kz58,526.41
You save:  Kz16,724.22
CLP$48,984.20
List Price:  CLP$68,581.80
You save:  CLP$19,597.60
CN¥361.44
List Price:  CN¥506.04
You save:  CN¥144.60
RD$2,960.45
List Price:  RD$4,144.87
You save:  RD$1,184.41
DA6,744.74
List Price:  DA9,443.17
You save:  DA2,698.43
FJ$113.05
List Price:  FJ$158.28
You save:  FJ$45.23
Q389.89
List Price:  Q545.87
You save:  Q155.98
GY$10,460.71
List Price:  GY$14,645.83
You save:  GY$4,185.12
ISK kr6,972.60
List Price:  ISK kr9,762.20
You save:  ISK kr2,789.60
DH506.54
List Price:  DH709.20
You save:  DH202.66
L881.58
List Price:  L1,234.28
You save:  L352.70
ден2,850.77
List Price:  ден3,991.30
You save:  ден1,140.53
MOP$402.90
List Price:  MOP$564.10
You save:  MOP$161.19
N$946.63
List Price:  N$1,325.35
You save:  N$378.72
C$1,840.39
List Price:  C$2,576.69
You save:  C$736.30
रु6,667.15
List Price:  रु9,334.54
You save:  रु2,667.39
S/185.40
List Price:  S/259.58
You save:  S/74.17
K191.34
List Price:  K267.89
You save:  K76.55
SAR187.48
List Price:  SAR262.49
You save:  SAR75
ZK1,261.25
List Price:  ZK1,765.85
You save:  ZK504.60
L230.69
List Price:  L322.98
You save:  L92.29
Kč1,175.26
List Price:  Kč1,645.46
You save:  Kč470.19
Ft18,362.67
List Price:  Ft25,709.21
You save:  Ft7,346.54
SEK kr535.59
List Price:  SEK kr749.87
You save:  SEK kr214.28
ARS$42,879.12
List Price:  ARS$60,034.20
You save:  ARS$17,155.08
Bs345.48
List Price:  Bs483.70
You save:  Bs138.22
COP$193,772.46
List Price:  COP$271,296.95
You save:  COP$77,524.48
₡25,413.63
List Price:  ₡35,581.11
You save:  ₡10,167.48
L1,234.27
List Price:  L1,728.07
You save:  L493.80
₲368,865.97
List Price:  ₲516,441.87
You save:  ₲147,575.90
$U1,890.76
List Price:  $U2,647.22
You save:  $U756.45
zł200.34
List Price:  zł280.50
You save:  zł80.15
Already have an account? Log In

Transcript

Hello everyone, in this video session I am going to explain Part Eight of selenium four that is using new window method for opening a new blank window or blank tab. So in Selenium for a new method has been introduced that is new window method okay is a new method introduced in Selenium four guys, what is the purpose of this new window method or command in Selenium for the purpose is to open a blank a new Okay, open a new blank tab or window. For example, guys, let me show you this thing visually. Okay. Let's say you have opened application like this. Okay, and now Now you want to open a new tab like this blank new tab.

Let's click on this place, this blank new tab will be open, or there may be some situations where you want to open a new blank window, okay while this original window is still open, okay, while you're still orginal window is still open, you want to open a new window like this, while your new original window is open like this, you want to open a new blank tab like this, okay. So to support that, to support that in Selenium for this new method, that is new window method got introduced guys using which we can open a new blank tab or new blank webpage or window. Okay. So and one more thing that you can do is till now ls today, okay till that is before Selenium four, you are able to open a single application and perform operations on this single web page or read this web page related to this application, right?

But what if I want to work simultaneously on multiple applications, for example, in one tab, I want to work on this omega.blogspot.com application. And in other tab, I want to work on another application like this. So simultaneously, I want to work here and work here and not simultaneously. It's like edit time in a single test, okay, in a single test, you want to work on this tab and let's say in this tab, you want to type some text here. And then in this, in this another application, you want to search for some iPhone, you can do that guys. Okay?

You can do that. In a single test case, you can do that. Okay by opening This new blank tab or a new blank window, okay, this is possible. So let me show you practically the same thing, how to use this new window method of selenium for for opening a new blank tab or blank window. Fine. So let me practically demonstrate for that let's open Eclipse ID.

So create a new Maven project. Okay, I already have this Maven project already. So this Maven project has this POM dot XML file where I have provided the dependency tax for this Selenium four. Okay, so already configured this Mavin Mavin project with the Selenium four guys and other required stuff, okay. To know how to configure project with Selenium four and other stuff. I already covered this as part of the first video of this Selenium four video series guys.

Okay. Go to the part one of the Selenium video series. And once you're done, okay, once you're done creating a Maven project and configuring that with the Selenium four, and other required things, then create a new class here, say new class and you ask for the class name. Let's do something like demo eight and select this public static void Main. So now the class with the main method got created inside the main method. Let's write some basic stuff like the WebDriver manager.

Dot driver dot setup. Then WebDriver. Let's open the Chrome browser is called New chrome driver. import this stuff WebDriver interface and then chrome driver class, just import that. Then, what's next? Then Maximize the browser window.

The basic steps guys, let's complete the basic step first. And let's provide some implicit wait in case when it is there it will reach. Okay, after this all the steps are done, now open the application URL. Let's say I want to open this sample application first. Okay, the sample application I want to open and provide here. And after opening the sample application, what I will do I will do some operations here.

Let's I will automate something let's say I want to type some text into this text field and click on the search button. Okay. So, so I already know the for further driver dot find element by dot name. Okay, you can actually slow down a bit, I would like to slow it down a bit. Let me inspect this and practically show you This is the HTML code for this text field. And it has some name locator name is equal to, I will say by dot name, I would like to enter some text into the text field.

So I will say by dot name, dot name. And just enter the value queue here. It is an invisible dq Yes, I just provide a cue here and then I will say dot sendkeys and entering some text into this text field. Then I'll give some text like cue fox.com. Okay, after entering the text here, like your fox.com Okay, so after doing this, what I want to do is I want to open a new tab like this. Okay, I want to open a new blank tab like this, how to do that.

So, using this method is new window command. So for that, what I have to do is driver dot Okay switch to thought new window you see, after switch to earlier we used to have only this since like switch to alert switch to Window Okay, we used to switch to window and switch to alert and switch to frame this kind of things used to be there now in Selenium for a new method has been added guys to the switch to that is new window method, okay. Select this and here we have to say window type, okay we have to type the exact window type text as you can see window type dot p open if you want open a new blank tab, then select this tab option okay window type dot tab. Okay, this will open a new blank tab will be opened Yes, okay. Okay, that's it.

And now if I say that's it, let's see, let's run this thing and let's practice Let's see like, first, this Chrome browser will open, the first window will open here in the Chrome browser, and this application will be loaded there. And thereafter, we will be typing some text into this gear fox.com text into the text field. And then a new tab will be open a new blank tab will be open. Okay, let's see that in action guys. So let's run this code. So the first window will open and launched and it will maximize in a while and it will load the application.

Then it will type some text into this text field. And then it has opened a new tab. As you see, a blank new tab has been opened here. From here to here, right. So now let's close this. Now guys.

If I say, Okay, if I say driver dot get then I'll do something like today. oriels mania.com slash demo or else I will directly go to this page, okay, copy this URL login page, I will just copy paste this into the gate. So what do you think what will happen guys okay, what will happen here? So, here guys first the first window will open here Chrome browser window then it will load this application will type this k false context into the one of the text field in this first application then it will switch to a new window okay A new window will be opened and the control will be switched. What I mean to say here is originally there is a window there was a window like this right this window is nothing but omega.blogspot.com. Okay, the window was there.

And after performing operations on this The moment you said okay, but I wonder switch to new and know what will happen Blank a blank I said this is a brand new tab will be open okay the same window a brand new tab will be open like this. So, where will be the control guys the control will go here this the driver control will go here okay as you have switched it to this new window you have clearly mentioned that driver dot switch to new window what happened here is your original driver control windows driver control was there here in the first window and once you switch it and open a new blank tab here the control has moved from here to here okay now the control is here. That means when you run this let me stop this okay. Let me comment this out. I will show you afterwards.

So when you run this originally the control is in this First window. By default the control is there in this first window. So it has loaded to the application and as the control is still there, it will type some text. But once it opens a new tab like this, the control has moved from this first window to this new tab. Okay, now the control is there here. Now if I say driver dot get at this moment after the control has been switched to this new window or new tab, once the control has switched to this new tab here, okay, if you say driver dot get this URL will be opened in the new database here it will be open not here.

Okay, it will be open here because the control is there in the new tab. Let's see that whether that is happening or not. Okay. This is the first window we will maximize and load the application and the control is still there In this first video. itself and the new tab has opened and where is the control now? The control is in the new tab guys as you can see this command got executed in the new tab okay new blank tab voltage what is good that means the control is there in this new tab okay the control has more here.

Now let me type some text into this search box field for that let me inspect let me inspect it again. Yeah, it has some name is about to saturate I will use the name locator here okay name is called researches. I'll do the same thing. I will say driver dot find element answer type some text into the search field okay. And element by dot name. Yes do that.

And God sent case I will say iPhone Okay, now run this. So we'll open the first window. Notice application, type some text here, open the new tab, load the second application as a focus has more to this new tab no driver control focus. Then after loading it will type text into this search field Okay, you see the control is there in this new tab now. Fine. Now, if I say driver dot close, what will happen?

Which tab will be closed either this tab or this tab that will close this okay. So either this tab or this tab which tab will be closed since the control is there in the new blank tab. Okay. This new tab since that underlays are in this new layout tab. This tab will be close like this. Okay, because of this driver dot close, the first tab will be the new tab will be close.

Let's see that in action. The new tab it will open the application and it will type some text into the search field and then it will close which tab has been closed. The new tab has been closed but the first window is there it's because the counter was there in the first tab, new tab. Now, since this new tab, new blank tab has been closed, new tab has been closed. We do think that that driver control has moved back to this first window since this See now only the first window is there, right? The data has been opened and closed.

Okay. Now do you think that the contract has moved here driver control has moved from here to here? The answer is no. Okay, the answer is no you can find it out. Or you can find it out. Let's say after closing the new tab, let's try to perform something on this.

Let's I want to type some text into this text area field. Okay, inspect this text area field. It has something like inspect it again. It has some ID guys, I will type some text into this text area will let's see what will happen. Driver dot find element by.id whatever the ID that I copied, and then I will say dot send keys. In this I'll type some text like you have cert comm Okay, so if the control is really good after after closing this new tab Control is automatically moved to this first window, then this statement should work, this statement should work.

If it is not working, that means that control is lost, okay, the control is lost and after after the new tab has been closed, the control is nowhere. Okay, something like that. So let's see that in action. If we type text into this text area field, then that's fine. Otherwise the candle is lost. Definitely the control slot will be lost guys.

So let's run this and see that you will get some exception now, as a driver control is not on the first window after closing the new tab. New Tab is open and open application another application you see multiple time multiple applications can be tested at same time right. So now you got an exception because no such vendor exception. You try to type something extended the first window as you assume that this new tab has been closed and the driver control how automatically moved to this first window you just assumed it, but it did happen right this date workout because the control has been lost. So you have to bring bring this driver control to this first window out of breakage. How to bring that the answer is simple guys.

So before what we call here when the control was in the first window, I'll write a statement here saying that driver dot get window handle okay. And I will store this window handle, this will return a string string, I will say first window handle first window and it is equal to when the candle was there in the in this code then the control was there. In the first window I'm just collecting this window and in of the current webpage that is the first window and storing it as possible. Window handle now after the control has been moved here and then it has the new tab has been closed okay before I will switch to this window okay after the after we have saved this window handle of this first window okay and before we perform something after closing the new tab and before performing again on the first window I will switch to this first window okay how to switch simple resolve this is a previous Selenium they also does their driver dot switch to dot okay window here you need to provide the first way to handle that the reason guys have stored this window handle here.

Okay, before we move to the before the control before this driver control most of this new tab why ended here is how stored the window handle of the first window into this variable okay first window handle variable and later point of time when the new tab has been closed and there was a requirement where how to bring back the control to this first window there have used driver dot switch to dot window hospital and that's it Okay. Now you can perform the operation manually. Now, I have manually brought to the control using the river dot switch to Window using the window handle or the first window I am just manually bringing the like forcibly bringing in a control to this main window first window and then if I perform operation this will work. So let's see whether this will work or not. run this. This will work guys.

So it has opened application new tab has been opened. Another application will be open Here, then we'll type some text and then we'll close the first one. And you'll see that in the the since the driver control came here back to the first window, you see that text gear fox.com has been typed into this text area field and you didn't get an exception here, right. So this is how guys you can open new tabs okay. new blank tabs using this new window command in Selenium for if in case instead of the tab if you want to open a window, okay. So, small differences If yes, comment this out.

And driver dot switch to dot new window here say dot window. Okay, that window so this will open a new blank window Okay, this will open a new blank window now. Save it and run this guy's instead of Tap new blank tab A new window will be open now. You see instead of a new tab, a blank new tab window got open and it'll open the application same things will happen close and then it will type so we brought the control back to the original window and then close and then type this text into the text field. So this is how guys we can work like we can open new tab, new blank tab or new blank window and work on multiple applications at the same time using this new window command. Okay, if you specify tab here it will open a new blank tab if you specify window here it will open a new window blank window.

So that's it guys. In the next video session, I'll explain and use one of the new features of selenium for that is relative locators. Okay, new country. related to locators in Selenium for that I'm going to explain in the next video session. So that's it guys. Thank you see in the next video session, bye

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.