Handling Multiple Windows With Protractor For Selenium Testing – DZone Performance | xxxHandling Multiple Windows With Protractor For Selenium Testing – DZone Performance – xxx
菜单

Handling Multiple Windows With Protractor For Selenium Testing – DZone Performance

四月 27, 2020 - MorningStar

Over a million developers have joined DZone.

  • Handling Multiple Windows With Protractor For Selenium Testing - DZone Performance

    {{node.title}}

    {{node.type}} · {{ node.urlSource.name }} by

    Download {{node.downloads}}

  • {{totalResults}} search results

{{announcement.body}}

{{announcement.title}}

Let’s be friends:

= 1024)” dc-slot=”ads.sl1.slot(articles[0], 0)” tags=”ads.sl1.tags(articles[0], 0)” size=”ads.sl1.size(articles[0], 0)” style=”border:0;”>
1 && !articles[0].partner.isSponsoringArticle && (width >= 1024)” dc-slot=”ads.sb2.slot(articles[0], 0)” tags=”ads.sb2.tags(articles[0], 0)” size=”ads.sb2.size(articles[0], 0)” is-nobid=”true”>

Handling Multiple Windows With Protractor For Selenium Testing

DZone ‘s Guide to

Handling Multiple Windows With Protractor For Selenium Testing

Learn how to handle multiple browser windows and tabs in Protractor framework for Selenium with examples in this complete guide. Read Now!

Jun. 05, 20 · Performance Zone ·

Free Resource

Join the DZone community and get the full member experience.

Join For Free

Every day is a challenge for newbie automation testers! Just when you learned how to perform automated browser testing on a single window, you now come across the challenge of handling multiple windows. Isn’t this a nightmare! Well, no need to worry, I’ve got you covered.

Handling Multiple Windows With Protractor For Selenium Testing - DZone Performance

While performing automated browser testing, at times you might end up in situations where you would need to handle multiple windows and tabs. Your test cases might need you to open a new tab and then switch back to the last window to complete the other pending activities. 

Let’s say you want to check if all the There might be a certain situation when you might come across a situation where the current browser tab is not active. Hence, to find these issues before they hamper your user’s experience, you need to make sure you include all the test cases involving multiple windows/tabs in Selenium with Protractor. 

In this Selenium Protractor tutorial, I’ll show you how to handle multiple windows in Selenium with Protractor. If you are not familiar with writing Selenium Automation tests on Protractor, I’ve already covered it in our previous Selenium Protractor tutorial.

Working With Browser Window Handles In Selenium With Protractor

Before we handle browser windows, we need to know how to identify different browser windows and tabs. It’s pretty straightforward, every browser or tab instance has a Global Unique Identifier(GUID), which makes it pretty easy to access them. GUID identifiers help to handle multiple windows, additionally, Protractor provides useful methods that can be used to handle windows. So, let’s have a look at these methods.

getWindowHandle() 

The getWindowHandle() function in the Protractor returns the string GUID for the current active window in the browser. This is invoked on the browser object as – browser.getWindowHandle();

getWindowHandles()

The getWindowHandles() function returns the GUID’s for all the windows and tabs opened on the active browser windows. This is called via the framework as browser.getWindowHandles();

switchTo() 

switchTo() method enables easy management of windows, tabs, and switch between the two. It shifts the control from the current window to the target windows. The GUID is passed as an argument to indicate the Protractor about the target window or tabs. i.e. browser.switchTo().window(guid);

How to Handle Multiple Browser Windows And Tabs In Selenium with Protractor?

Now that you know how to get the GUID of the browser windows and the methods to handle them, you should be fine. So, let’s look at this issue in more detail with a step by step approach to handle multiple windows in the browser.

For ease of understanding, I will start by handling two windows.

Steps To Handle Two Windows In The Browser:

1. First, let’s open the window and launch the URL https://www.lambdatest.com in the browser, to handle multiple windows in Selenium for automated browser testing.

JavaScript

 

x
 

1

// this property is used to assign an implicit wait time to 20 seconds for automated browser testing  //

2

browser.manage().timeouts().implicitlyWait(20000);

3

 

4

// this functions redirects to the url in the browser for automated browser testing //

5

browser.get(" https://www.lambdatest.com ");

6

 

2.  Then by using Protractor’s getWindowHandle() function, I’ll get the GUID of the currently active window and store it into a variable.

JavaScript

 

xxxxxxxxxx
1

 

1

// it fetches the unique identifier for the first window for automated browser testing  //

2

 

3

browser.getWindowHandle().then(function(firstGUID){

3.  Similarly, you can open a new tab/window and navigate to a new URL  e.g. https://www.google.com. Further, by using the sleep method, I can indicate to the Protractor to wait for some time, let’s say 4 seconds, to allow the new tab to load the URL completely. You can even use implicit and explicit wait commands depending on the requirement.

JavaScript

 

browser.manage().timeouts().implicitlyWait(20000);

0

1

 

1

browser.manage().timeouts().implicitlyWait(20000);

1

2

browser.manage().timeouts().implicitlyWait(20000);

2

3

browser.manage().timeouts().implicitlyWait(20000);

3

4

browser.manage().timeouts().implicitlyWait(20000);

4

5

browser.manage().timeouts().implicitlyWait(20000);

5

6

browser.manage().timeouts().implicitlyWait(20000);

6

4. Next, I’ll get the GUIDs for both the windows i.e. the first window (parent windows) as well as the seconds’ window ( newly opened windows ), using the Protractor’s getWindowHandles() function, which stores the guide values for both windows in a collection Set.

JavaScript

 

browser.manage().timeouts().implicitlyWait(20000);

7

1

 

1

browser.manage().timeouts().implicitlyWait(20000);

8

2

browser.manage().timeouts().implicitlyWait(20000);

9

5. Next, let’s perform an iteration over this collection of GUID, and switch to the new window only after encountering a new GUID, to add to the collection.

JavaScript

 

 

0

1

 

1

 

1

2

 

2

3

 

3

4

 

4

5

 

5

6

 

6

7

 

7

6. Now, I’ll switch to the new window by using the switch().window() function and pass the GUID of the new window as an argument to this function.

JavaScript

 

 

8

1

 

1

 

9

2

// this functions redirects to the url in the browser for automated browser testing //

0

3

// this functions redirects to the url in the browser for automated browser testing //

1

4

// this functions redirects to the url in the browser for automated browser testing //

2

5

// this functions redirects to the url in the browser for automated browser testing //

3

7. Finally, let’s perform a search query on Google, after this, I’ll close the window and return to the previous window.

JavaScript

 

// this functions redirects to the url in the browser for automated browser testing //

4

1

 

1

// this functions redirects to the url in the browser for automated browser testing //

5

2

// this functions redirects to the url in the browser for automated browser testing //

6

3

// this functions redirects to the url in the browser for automated browser testing //

7

4

// this functions redirects to the url in the browser for automated browser testing //

8

5

// this functions redirects to the url in the browser for automated browser testing //

9

6

browser.get(" https://www.lambdatest.com ");

0

7

browser.get(" https://www.lambdatest.com ");

1

8

browser.get(" https://www.lambdatest.com ");

2

In the automation test script below, I’ve taken a test case to show you how to handle and switch between two windows in a browser.

test_config.js

This is the configuration file used by Protractor to manage any config parameter, used globally within the application.

JavaScript

 

browser.get(" https://www.lambdatest.com ");

3

1

86

 

1

browser.get(" https://www.lambdatest.com ");

4

2

browser.get(" https://www.lambdatest.com ");

5

3

browser.get(" https://www.lambdatest.com ");

6

4

browser.get(" https://www.lambdatest.com ");

7

5

browser.get(" https://www.lambdatest.com ");

8

6

browser.get(" https://www.lambdatest.com ");

9

7

 

0

8

 

1

9

 

2

10

 

3

11

 

4

12

 

5

13

 

6

14

 

7

15

 

8

16

 

9

17

xxxxxxxxxx

0

18

xxxxxxxxxx

1

19

xxxxxxxxxx

2

20

xxxxxxxxxx

3

21

xxxxxxxxxx

4

22

xxxxxxxxxx

5

23

xxxxxxxxxx

6

24

xxxxxxxxxx

7

25

xxxxxxxxxx

8

26

xxxxxxxxxx

9

27

// it fetches the unique identifier for the first window for automated browser testing  //

0

28

// it fetches the unique identifier for the first window for automated browser testing  //

1

29

// it fetches the unique identifier for the first window for automated browser testing  //

2

30

// it fetches the unique identifier for the first window for automated browser testing  //

3

31

// it fetches the unique identifier for the first window for automated browser testing  //

4

32

// it fetches the unique identifier for the first window for automated browser testing  //

5

33

// it fetches the unique identifier for the first window for automated browser testing  //

6

34

// it fetches the unique identifier for the first window for automated browser testing  //

7

35

// it fetches the unique identifier for the first window for automated browser testing  //

8

36

// it fetches the unique identifier for the first window for automated browser testing  //

9

37

 

0

38

 

1

39

 

2

40

 

3

41

 

4

42

 

5

43

 

6

44

 

7

45

 

8

46

 

9

47

browser.getWindowHandle().then(function(firstGUID){

0

48

browser.getWindowHandle().then(function(firstGUID){

1

49

browser.getWindowHandle().then(function(firstGUID){

2

50

browser.getWindowHandle().then(function(firstGUID){

3

51

browser.getWindowHandle().then(function(firstGUID){

4

52

browser.getWindowHandle().then(function(firstGUID){

5

53

browser.getWindowHandle().then(function(firstGUID){

6

54

browser.getWindowHandle().then(function(firstGUID){

7

55

browser.getWindowHandle().then(function(firstGUID){

8

56

browser.getWindowHandle().then(function(firstGUID){

9

57

browser.manage().timeouts().implicitlyWait(20000);

00

58

browser.manage().timeouts().implicitlyWait(20000);

01

59

browser.manage().timeouts().implicitlyWait(20000);

02

60

browser.manage().timeouts().implicitlyWait(20000);

03

61

browser.manage().timeouts().implicitlyWait(20000);

04

62

browser.manage().timeouts().implicitlyWait(20000);

05

63

browser.manage().timeouts().implicitlyWait(20000);

06

64

browser.manage().timeouts().implicitlyWait(20000);

07

65

browser.manage().timeouts().implicitlyWait(20000);

08

66

browser.manage().timeouts().implicitlyWait(20000);

09

67

browser.manage().timeouts().implicitlyWait(20000);

10

68

browser.manage().timeouts().implicitlyWait(20000);

11

69

browser.manage().timeouts().implicitlyWait(20000);

12

70

browser.manage().timeouts().implicitlyWait(20000);

13

71

browser.manage().timeouts().implicitlyWait(20000);

14

72

browser.manage().timeouts().implicitlyWait(20000);

15

73

browser.manage().timeouts().implicitlyWait(20000);

16

74

browser.manage().timeouts().implicitlyWait(20000);

17

75

browser.manage().timeouts().implicitlyWait(20000);

18

76

browser.manage().timeouts().implicitlyWait(20000);

19

77

browser.manage().timeouts().implicitlyWait(20000);

20

78

browser.manage().timeouts().implicitlyWait(20000);

21

79

browser.manage().timeouts().implicitlyWait(20000);

22

80

browser.manage().timeouts().implicitlyWait(20000);

23

81

browser.manage().timeouts().implicitlyWait(20000);

24

82

browser.manage().timeouts().implicitlyWait(20000);

25

83

browser.manage().timeouts().implicitlyWait(20000);

26

84

browser.manage().timeouts().implicitlyWait(20000);

27

85

browser.manage().timeouts().implicitlyWait(20000);

28

86

browser.manage().timeouts().implicitlyWait(20000);

29

Now let’s take a scenario, where you have to handle more than two windows for automated browser testing. This approach is slightly different, I’ll use three windows for Selenium test automation in this case. You can repeat the same process for any number of browsers you want to work with.

Steps to Handle More Than Two Windows In the Browser With Selenium and Protractor

  1. Start with performing  Step 1 to Step 3 from the previous test case.

  2. Now, you already have two tabs, open the third tab, let’s open https://www.selenium.dev, and use the sleep function for 4 seconds to wait for the new tab to load completely. 

  3. Now we will store the GUID values for all the three windows in the collection Set  including the value of the newly opened windows using the Protractor’s getWindowHandles() function.

  4. Finally, I’ll iterate through the collection and switch to the second tab i.e. https://www.lambdatest.com using the switchTo() method, and then I’ll verify the title of the web page to be  “Cross Browser Testing Tools”.

  5. You can now perform any action on the tab and then close the window to return to the last opened window or tab.

Below is the Selenium test automation script to demonstrate how to handle and switch between more than two windows in a browser in Selenium with Protractor.

JavaScript

 

browser.manage().timeouts().implicitlyWait(20000);

30

1

48

 

1

browser.manage().timeouts().implicitlyWait(20000);

31

2

browser.manage().timeouts().implicitlyWait(20000);

32

3

browser.manage().timeouts().implicitlyWait(20000);

33

4

browser.manage().timeouts().implicitlyWait(20000);

34

5

browser.manage().timeouts().implicitlyWait(20000);

35

6

browser.manage().timeouts().implicitlyWait(20000);

36

7

browser.manage().timeouts().implicitlyWait(20000);

37

8

browser.manage().timeouts().implicitlyWait(20000);

38

9

browser.manage().timeouts().implicitlyWait(20000);

39

10

browser.manage().timeouts().implicitlyWait(20000);

40

11

browser.manage().timeouts().implicitlyWait(20000);

41

12

browser.manage().timeouts().implicitlyWait(20000);

42

13

browser.manage().timeouts().implicitlyWait(20000);

43

14

browser.manage().timeouts().implicitlyWait(20000);

44

15

browser.manage().timeouts().implicitlyWait(20000);

45

16

browser.manage().timeouts().implicitlyWait(20000);

46

17

browser.manage().timeouts().implicitlyWait(20000);

47

18

browser.manage().timeouts().implicitlyWait(20000);

48

19

browser.manage().timeouts().implicitlyWait(20000);

49

20

browser.manage().timeouts().implicitlyWait(20000);

50

21

browser.manage().timeouts().implicitlyWait(20000);

51

22

browser.manage().timeouts().implicitlyWait(20000);

52

23

browser.manage().timeouts().implicitlyWait(20000);

53

24

browser.manage().timeouts().implicitlyWait(20000);

54

25

browser.manage().timeouts().implicitlyWait(20000);

55

26

browser.manage().timeouts().implicitlyWait(20000);

56

27

browser.manage().timeouts().implicitlyWait(20000);

57

28

browser.manage().timeouts().implicitlyWait(20000);

58

29

browser.manage().timeouts().implicitlyWait(20000);

59

30

browser.manage().timeouts().implicitlyWait(20000);

60

31

browser.manage().timeouts().implicitlyWait(20000);

61

32

browser.manage().timeouts().implicitlyWait(20000);

62

33

browser.manage().timeouts().implicitlyWait(20000);

63

34

browser.manage().timeouts().implicitlyWait(20000);

64

35

browser.manage().timeouts().implicitlyWait(20000);

65

36

browser.manage().timeouts().implicitlyWait(20000);

66

37

browser.manage().timeouts().implicitlyWait(20000);

67

38

browser.manage().timeouts().implicitlyWait(20000);

68

39

browser.manage().timeouts().implicitlyWait(20000);

69

40

browser.manage().timeouts().implicitlyWait(20000);

70

41

browser.manage().timeouts().implicitlyWait(20000);

71

42

browser.manage().timeouts().implicitlyWait(20000);

72

43

browser.manage().timeouts().implicitlyWait(20000);

73

44

browser.manage().timeouts().implicitlyWait(20000);

74

45

browser.manage().timeouts().implicitlyWait(20000);

75

46

browser.manage().timeouts().implicitlyWait(20000);

76

47

browser.manage().timeouts().implicitlyWait(20000);

77

48

browser.manage().timeouts().implicitlyWait(20000);

78

Additional Features to Handle Multiple Browser Windows In Selenium With Protractor

The Protractor framework is so robust that it provides various customized utilities that allow us to perform certain actions, by calling its inbuilt classes and functions on the browser object. For instance, imagine a scenario where there is a requirement to open a link in the new window but as a standard protocol, the browser does not allow to open the link in the new window by default. In such a case, we need to force open the link in the new window, using Protractor’s action class.

Below are the steps by step approach to open the link in such a use case :

1. We need to specify the URL we would want to launch ( https:// www.google.com ) and store the element id “force-new-window” for the above link to a variable of type WebElement.

JavaScript

 

browser.manage().timeouts().implicitlyWait(20000);

79

1

 

1

browser.manage().timeouts().implicitlyWait(20000);

80

2

browser.manage().timeouts().implicitlyWait(20000);

81

2. Next, we will create an object of the action class to invoke certain events on the hyperlink.

JavaScript

 

browser.manage().timeouts().implicitlyWait(20000);

82

1

 

1

browser.manage().timeouts().implicitlyWait(20000);

83

2

browser.manage().timeouts().implicitlyWait(20000);

84

3

browser.manage().timeouts().implicitlyWait(20000);

85

3. Then, we will call various methods on the action class object

  1. Invoke the keyDown () function and pass the Shift key as an argument.

  2. Invoke the click () function and pass the web element from above as an argument.

  3. Finally, we will bind these two methods to the action class using the perform function and our task gets executed. Now we can see the website launched in a new window.

JavaScript

 

browser.manage().timeouts().implicitlyWait(20000);

86

1

 

1

browser.manage().timeouts().implicitlyWait(20000);

87

2

browser.manage().timeouts().implicitlyWait(20000);

88

3

browser.manage().timeouts().implicitlyWait(20000);

89

4

browser.manage().timeouts().implicitlyWait(20000);

90

Below is the complete script that demonstrates how Protractor forcefully launches the link in a new browser window.

JavaScript

 

browser.manage().timeouts().implicitlyWait(20000);

91

1

28

 

1

browser.manage().timeouts().implicitlyWait(20000);

92

2

browser.manage().timeouts().implicitlyWait(20000);

93

3

browser.manage().timeouts().implicitlyWait(20000);

94

4

browser.manage().timeouts().implicitlyWait(20000);

95

5

browser.manage().timeouts().implicitlyWait(20000);

96

6

browser.manage().timeouts().implicitlyWait(20000);

97

7

browser.manage().timeouts().implicitlyWait(20000);

98

8

browser.manage().timeouts().implicitlyWait(20000);

99

9

 

00

10

 

01

11

 

02

12

 

03

13

 

04

14

 

05

15

 

06

16

 

07

17

 

08

18

 

09

19

 

10

20

 

11

21

 

12

22

 

13

23

 

14

24

 

15

25

 

16

26

 

17

27

 

18

28

 

19

Enable Hyperlink to Open in A New Window

Usually, while clicking a hyperlink opens on the same window by default. 

JavaScript

 

 

20

1

 

1

 

21

But there are certain scenarios when you need the flexibility to open the link in the new windows rather than the same window or tab. This can be done simply by using the anchor tag and setting the value of the target keyword as “_blank”. This ensures that whenever a user clicks on the link, it launches in the new window.

JavaScript

 

 

22

1

 

1

 

23

2

 

24

3

 

25

4

 

26

5

 

27

Execute Automation Scripts on Online Selenium Grid Platform With Protractor

You can execute the same Selenium test automation in the cloud Selenium grid platform with minimal configuration changes that are required to build the driver and connect to the LambdaTest hub. Below is the updated script with the required changes

test_config.js

JavaScript

 

 

28

1

47

 

1

 

29

2

 

30

3

 

31

4

 

32

5

 

33

6

 

34

7

 

35

8

 

36

9

 

37

10

 

38

11

 

39

12

 

40

13

 

41

14

 

42

15

 

43

16

 

44

17

 

45

18

 

46

19

 

47

20

 

48

21

 

49

22

 

50

23

 

51

24

 

52

25

 

53

26

 

54

27

 

55

28

 

56

29

 

57

30

 

58

31

 

59

32

 

60

33

 

61

34

 

62

35

 

63

36

 

64

37

 

65

38

 

66

39

 

67

40

 

68

41

 

69

42

 

70

43

 

71

44

 

72

45

 

73

46

 

74

47

 

75

test_script.js

JavaScript

 

 

76

1

44

 

1

 

77

2

 

78

3

 

79

4

 

80

5

 

81

6

 

82

7

 

83

8

 

84

9

 

85

10

 

86

11

 

87

12

 

88

13

 

89

14

 

90

15

 

91

16

 

92

17

 

93

18

 

94

19

 

95

20

 

96

21

 

97

22

 

98

23

 

99

24

// this functions redirects to the url in the browser for automated browser testing //

00

25

// this functions redirects to the url in the browser for automated browser testing //

01

26

// this functions redirects to the url in the browser for automated browser testing //

02

27

// this functions redirects to the url in the browser for automated browser testing //

03

28

// this functions redirects to the url in the browser for automated browser testing //

04

29

// this functions redirects to the url in the browser for automated browser testing //

05

30

// this functions redirects to the url in the browser for automated browser testing //

06

31

// this functions redirects to the url in the browser for automated browser testing //

07

32

// this functions redirects to the url in the browser for automated browser testing //

08

33

// this functions redirects to the url in the browser for automated browser testing //

09

34

// this functions redirects to the url in the browser for automated browser testing //

10

35

// this functions redirects to the url in the browser for automated browser testing //

11

36

// this functions redirects to the url in the browser for automated browser testing //

12

37

// this functions redirects to the url in the browser for automated browser testing //

13

38

// this functions redirects to the url in the browser for automated browser testing //

14

39

// this functions redirects to the url in the browser for automated browser testing //

15

40

// this functions redirects to the url in the browser for automated browser testing //

16

41

// this functions redirects to the url in the browser for automated browser testing //

17

42

// this functions redirects to the url in the browser for automated browser testing //

18

43

// this functions redirects to the url in the browser for automated browser testing //

19

44

// this functions redirects to the url in the browser for automated browser testing //

20

You can execute the Selenium test automation scripts on the cloud, just by adding the desired capability to your code, to connect to the LambdaTest platform. To perform automated browser testing on the cloud Selenium Grid, you need to generate the desired capability matrix to specify the environment you want to execute our Selenium test automation on. Here is the link to visit LambdaTest Selenium desired capabilities generator.

Below is the output on running the Selenium test automation:

To know more about how to handle mouse action and keyboard events in Protractor, refer to our blog on the topic.

Wrapping It Up!

This is just the end of the beginning! You now know how to handle multiple browser windows. Go ahead, and handle the heck out of these browser windows. They might have been a nightmare before, but between you and me, we now know that it’s just a piece of cake! You now know how to handle browser windows and tabs in the Protractor framework to perform Selenium Automation Testing. The framework is robust, flexible, and provides various inbuilt classes and functions. 

Handling Multiple Windows With Protractor For Selenium Testing - DZone Performance

In case, you want to learn more about using Protractor with Selenium, do subscribe to our blog. And, I’d keep you posted about new blogs. This is certainly not the last blog on Protractor, there’s more to come! If you haven’t checked out our previous blogs on cross-browser testing with a protractor, and Selenium locators with a protractor, debug protractor tests and handling alerts and popups.  I’d urge you to do so, as these topics are essential for automated browser testing. Also, If you’d like to help your peers learn about handling multiple browsers, do share this blog with them. 

That’s all for now!

Happy Testing.

Like This Article? Read More From DZone

Topics:
javascript ,javascript tutorial ,performance ,protractor ,selenium automated testing

Published at DZone with permission of Aditya Dwivedi . See the original article here.

Opinions expressed by DZone contributors are their own.

Performance Partner Resources

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.linkDescription }}

{{ parent.urlSource.name }}

by

CORE

· {{ parent.articleDate | date:’MMM. dd, yyyy’ }} {{ parent.linkDate | date:’MMM. dd, yyyy’ }}


Notice: Undefined variable: canUpdate in /var/www/html/wordpress/wp-content/plugins/wp-autopost-pro/wp-autopost-function.php on line 51