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
€47.98
List Price:  €67.18
You save:  €19.19
£39.89
List Price:  £55.85
You save:  £15.96
CA$70.14
List Price:  CA$98.21
You save:  CA$28.06
A$76.87
List Price:  A$107.62
You save:  A$30.75
S$67.31
List Price:  S$94.24
You save:  S$26.93
HK$389.14
List Price:  HK$544.83
You save:  HK$155.68
CHF 44.67
List Price:  CHF 62.54
You save:  CHF 17.87
NOK kr553.51
List Price:  NOK kr774.97
You save:  NOK kr221.45
DKK kr357.84
List Price:  DKK kr501
You save:  DKK kr143.16
NZ$85.68
List Price:  NZ$119.95
You save:  NZ$34.27
د.إ183.61
List Price:  د.إ257.07
You save:  د.إ73.46
৳5,972.22
List Price:  ৳8,361.58
You save:  ৳2,389.36
₹4,221.07
List Price:  ₹5,909.84
You save:  ₹1,688.76
RM223.35
List Price:  RM312.71
You save:  RM89.36
₦84,627.22
List Price:  ₦118,484.88
You save:  ₦33,857.66
₨13,887.22
List Price:  ₨19,443.22
You save:  ₨5,556
฿1,722.96
List Price:  ฿2,412.28
You save:  ฿689.32
₺1,727.27
List Price:  ₺2,418.32
You save:  ₺691.05
B$289.99
List Price:  B$406.01
You save:  B$116.02
R907.60
List Price:  R1,270.71
You save:  R363.11
Лв93.82
List Price:  Лв131.35
You save:  Лв37.53
₩70,211.45
List Price:  ₩98,301.65
You save:  ₩28,090.20
₪185.71
List Price:  ₪260.01
You save:  ₪74.30
₱2,946.36
List Price:  ₱4,125.14
You save:  ₱1,178.78
¥7,736.95
List Price:  ¥10,832.35
You save:  ¥3,095.40
MX$1,021.22
List Price:  MX$1,429.79
You save:  MX$408.57
QR182.26
List Price:  QR255.18
You save:  QR72.92
P683.46
List Price:  P956.90
You save:  P273.44
KSh6,472.14
List Price:  KSh9,061.51
You save:  KSh2,589.37
E£2,482.01
List Price:  E£3,475.01
You save:  E£993
ብር6,118.22
List Price:  ብር8,566
You save:  ብር2,447.77
Kz45,640.87
List Price:  Kz63,900.87
You save:  Kz18,260
CLP$48,886.48
List Price:  CLP$68,444.99
You save:  CLP$19,558.50
CN¥362.07
List Price:  CN¥506.93
You save:  CN¥144.86
RD$3,012.01
List Price:  RD$4,217.06
You save:  RD$1,205.04
DA6,712.40
List Price:  DA9,397.90
You save:  DA2,685.50
FJ$113.77
List Price:  FJ$159.29
You save:  FJ$45.51
Q385.78
List Price:  Q540.13
You save:  Q154.34
GY$10,455.70
List Price:  GY$14,638.82
You save:  GY$4,183.11
ISK kr6,980.70
List Price:  ISK kr9,773.54
You save:  ISK kr2,792.83
DH502.76
List Price:  DH703.91
You save:  DH201.14
L910.90
List Price:  L1,275.33
You save:  L364.43
ден2,951.80
List Price:  ден4,132.76
You save:  ден1,180.95
MOP$400.70
List Price:  MOP$561.01
You save:  MOP$160.31
N$906.31
List Price:  N$1,268.91
You save:  N$362.60
C$1,838.97
List Price:  C$2,574.70
You save:  C$735.73
रु6,749.45
List Price:  रु9,449.77
You save:  रु2,700.32
S/189.51
List Price:  S/265.32
You save:  S/75.81
K201.21
List Price:  K281.71
You save:  K80.50
SAR187.68
List Price:  SAR262.77
You save:  SAR75.08
ZK1,382
List Price:  ZK1,934.92
You save:  ZK552.91
L238.86
List Price:  L334.42
You save:  L95.56
Kč1,216.06
List Price:  Kč1,702.59
You save:  Kč486.52
Ft19,746.05
List Price:  Ft27,646.05
You save:  Ft7,900
SEK kr551.69
List Price:  SEK kr772.42
You save:  SEK kr220.72
ARS$50,176.71
List Price:  ARS$70,251.41
You save:  ARS$20,074.70
Bs345.34
List Price:  Bs483.50
You save:  Bs138.16
COP$219,443.60
List Price:  COP$307,238.59
You save:  COP$87,794.99
₡25,456.77
List Price:  ₡35,641.51
You save:  ₡10,184.74
L1,262.95
List Price:  L1,768.23
You save:  L505.28
₲390,155.46
List Price:  ₲546,248.87
You save:  ₲156,093.40
$U2,130.69
List Price:  $U2,983.14
You save:  $U852.45
zł208.13
List Price:  zł291.40
You save:  zł83.27
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.