# Browser Library - Switch Browser example?

**URL:** <https://forum.robotframework.org/t/browser-library-switch-browser-example/2296>\
**Category:** Browser\
**Created:** [18 August 2021 11:13 UTC](https://forum.robotframework.org/t/browser-library-switch-browser-example/2296 "2021-08-18T11:13:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mikap72](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/mikap72/32/966_2.png) [@mikap72](https://forum.robotframework.org/u/mikap72)\
**Post date:** [18 August 2021 11:13 UTC](https://forum.robotframework.org/t/browser-library-switch-browser-example/2296/1 "2021-08-18T11:13:26Z")

</div>

Hi,  
could someone post example code for creating two browser instances and switching between those using Robot Framework’s Browser (NOT using SeleniumLibrary) library?

Namely: how to obtain correct browser id for Switch Browser keyword.

`Switch Browser ${ibrowser1_id}`

Full code so far:

```
Library Browser
Library String
Library Collections

**Variables**
${url1}= https://www.google.com
${url1}= https://www.bing.com
@{browser_ids}= Create List

**Test Cases**
Open Browser 1
    New Browser chromium headless=false
    New Context viewport={'width': 1920, 'height': 1080}
    New Page ${url1}

Open Browser 1
    New Browser chromium headless=false
    New Context viewport={'width': 1920, 'height': 1080}
    New Page ${url2}

```

Page Switch tests

```
[Tags] UI dev

Switch to Browser1
Get Element Gmail
Switch to Browser2
Get Element © 2021 Microsoft

**Keywords**
Switch to Browser2
    Switch Page ${browser_ids}[1]

Switch to Browser1
    Switch Page ${browser_ids}[0]

Store Active Browser Id
    ${active_id}= Get Page Ids ACTIVE ACTIVE ACTIVE
    Log To Console ${active_id}
    ${active_id}= Split String ${active_id}[0] =
    Log To Console ${active_id}[1]
    Append To List ${BROWSER_IDS} ${active_id}
```

---

<div class="post-metadata">

**Author:** ![mikap72](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/mikap72/32/966_2.png) [@mikap72](https://forum.robotframework.org/u/mikap72)\
**Post date:** [18 August 2021 14:17 UTC](https://forum.robotframework.org/t/browser-library-switch-browser-example/2296/2 "2021-08-18T14:17:28Z")

</div>

Browser library has automatic teardown for Browser/Context/Page instances, so you have to create all needed instances of those in Suite Setup. Solution below:

```
	***Settings***
	Library Browser
	#Library OperatingSystem
	Library Collections
	Library String
	Suite Setup Create Suite Ecosystem

	***Variables***

	@{bids}
	@{cids}
	@{pids}
	${url1}= https://www.google.com
	${url2}= https://www.bing.com

	***Test Cases***

	Setup Verification
		[Tags] UI dev
		${bids}= Get Browser Ids
		Log To console ${bids}

		${bids}= Get Context Ids
		Log To console ${cids}

		${pids}= Get Page Ids
		Log To console ${pids}

	Page Switch tests
		[Tags] UI dev
		Switch to Page 1
		Get Element text=Gmail
		Switch to Page 2
		Get Element text=© 2021 Microsoft

	**Keywords**

	Switch to Page 1
		Switch Page ${pids}[0]

	Switch to Page 2
		Switch Page ${pids}[1]

	Store Active Browser Id
		${active_id}= Get Browser Ids ACTIVE
		#${active_id}= Split String ${active_id}[0] =
		Log To Console ${active_id}[0]
		Append To List ${bids} ${active_id}[0]

	Store Active Context Id
		${active_id}= Get Context Ids ACTIVE ACTIVE
		Log To Console ${active_id}
		# ${active_id}= Split String ${active_id}[0] =
		Log To Console ${active_id}[0]
		Append To List ${cids} ${active_id}[0]

	Store Active Page Id
		[Arguments] ${new_id}
		${active_id}= Get Page Ids ACTIVE ACTIVE ACTIVE
		# ${active_id}= Split String ${active_id}[0] =
		Log To Console ${active_id}[0]
		Append To List ${pids} ${new_id}

	Create Suite Ecosystem
		# Create 1B-1C-2P Ecosystem

		# 1st Browser
		New Browser chromium headless=false
		Store Active Browser Id

		# 1st Context of 1st Browser
		New Context viewport={'width': 1920, 'height': 1080}
		Store Active Context Id

		# 1st Page of 1st Context of 1st Context
		New Page ${url1}
		${current}= Switch Page CURRENT
		Store Active Page Id ${current}

		# 2nd Page of 1st Context of 1st Context
		New Page ${url2}
		${current}= Switch Page CURRENT
		Store Active Page Id ${current}
```

---

<div class="post-metadata">

**Author:** ![serici](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/serici/32/446_2.png) [@serici](https://forum.robotframework.org/u/serici)\
**Post date:** [20 August 2021 08:34 UTC](https://forum.robotframework.org/t/browser-library-switch-browser-example/2296/3 "2021-08-20T08:34:59Z")

</div>

Hi, in my project i’ve done like this:

First i open all my test cases with a template keyword

```
***Test Cases***
OpenAllBrowser
     [Documentation] loop on browsers
     [Template] OpenToDesigner
     firefox ${INVISIBLE_BROWSER} ${SCREEN_SIZE} fr_FR google.fr
     chromium ${INVISIBLE_BROWSER} ${SCREEN_SIZE} ${LANGUAGE} bing.fr

```

`Open to designer` keyword just call `New Browser` and `New Context` and `New Page`

this test case is in a file in an upper directory. All my real tests are in sub directories and start with a loop

```
***Test Cases***
ParseAllBrowser
   [Documentation] loop on browsers and test each keywords
   ${browserList} Get Browser Ids
   FOR ${browserInstance} IN @{browserList}
       CreatePlaformeByMenu ${browserInstance}
   END

```

in all keyword i have the command

```
Switch Browser ${browserUUID}

```

is that help ?

---

<div class="post-metadata">

**Author:** ![mikap72](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/mikap72/32/966_2.png) [@mikap72](https://forum.robotframework.org/u/mikap72)\
**Post date:** [20 August 2021 08:50 UTC](https://forum.robotframework.org/t/browser-library-switch-browser-example/2296/4 "2021-08-20T08:50:04Z")

</div>

Mikko Korpela from Browser development team answered this in StackOverflow:

> <https://stackoverflow.com/questions/68831400/how-to-switch-browsers-in-robot-framework-using-browser-library/68838768#68838768>
