Hello,
I’ll continue more about testing with JavaScript WD test frame and Selenium stand-alone server.
Today’s topic is navigating between browser windows (or tabs) with WD.

The Webdriver sees each tab and window as a window, it doesn’t make difference. The difference is only at the browser how it is set to handle the windows appearance, opens them in separate browser or as tabs. With WD the navigation is pretty straight forward and not so difficult. There is a command called windowHandles() which as you can read handles the windows. Actually it returns a list of opened windows based on a generated ID by the browser. I’ll give some minor example about navigating and work between two windows.

Few other commands which are here to help for window manipulation:

get(URL) – opens the desired URL in browser, initial window
newWindow(URL) – opens the site given by the URL in a new window
window(ID) – jumps to desired window by the generated ID

it('TestCase: Navigation between browser pages', function(done) {
	driver.run(function*() {
	
		//open the desired site first, initial window/tab
		yield this.get(yourFirstSiteUrl);
		//wait 5 seconds to load (you can use some other wait method, it's advised)
		yield this.sleep(5);
		//open the second site by URL in new window/tab (depends on browser settings)
		yield this.newWindow(yourSecondSiteUrl);
		
		//get the list of opened windows
		var handles = yield this.windowHandles();
		
		//get the generated ID's of the windows
		var firstWindow = handles[0];
		var secondWindow = handles[1];
	
		//jump to the second window
		yield this.window(secondWindow);
		yield this.sleep(5);
		
		/*
		********************
		* Do your work on the page, fetch data, check element visibility, your choice...
		********************
		*/
		
		//close the window/tab
		yield this.close();
		
		// navigate back to the initial page
		yield this.window(firstWindow);
		yield this.sleep(5);
		
		/**
		********************
		* Do your work on the page, compare data, check element visibility, your choice...
		********************
		*/
		
		//when you're done with work, exit
		done();
	});
});

This is just a small example to give you a head start, there is plentiful to test with multiple pages and to play around it. I leave this up to the reader, to use it as it pleases.

Thank You for reading this.

Similar Posts from the author: