The browser.url(url) command opens the specified URL in the browser instance. The command takes a single string type parameter that is usually a URL of application under test
browser.url('chercher.tech/practice/popups')
Whenever we pass the website address to the get() method. It first of checks for the protocol in the address, but in the above program we didnot mention any the protocol.
What is protocol ?
Protocol is simple it mention what kind of site is that.
it('Get Command with WebdriverIO', () => {
browser.url('https://chercher.tech/practice/popups')
});
The getTitle()
command is used to retrieve the title of the webpage the user is currently working on. A null string is returned if the webpage has no title. The command doesn’t require any parameter and returns a trimmed string value
browser.getTitle()
The browser.getUrl()
command is used to retrieve the URL of the webpage the user is currently accessing. The command doesn't require any parameter and returns a string value
browser.getUrl()
The getPageSource()
command is used to retrieve the page source of the webpage the user is currently accessing. The command doesn’t require any parameter and returns a string value
browser.getPageSource()
Complete code
it('get page title with WebdriverIO', () => {
browser.url('https://chercher.tech/practice/popups')
console.log(browser.getTitle());
console.log(browser.getUrl());
console.log(browser.getPageSource());
});
In WebdriverIO as we open a browser sometime it may not open on full screen, thus it is a good practice to maximise a browser at time it open.
WebdriverIO provides maximizeWindow()
function to maximize the current browser window.
browser.maximizeWindow()
Sometimes you need to set window position and size or get window size and position. In selenium software test. WebdriverIO software testing tool has many useful methods using which we can play with web browser window for different purpose.
We can use setWindowSize()
function to set the size of window in webdriverio. We can set window width to 400 and height to 500 dimensions using bellow given syntax
browser.setWindowSize(400, 500)
We can use getWindowSize()
method to get size of window. Below given syntax will return window height and width.
it('Browser Commands with WebdriverIO', () => {
browser.url('https://chercher.tech/practice/popups')
browser.setWindowSize(400, 500)
console.log(browser.getWindowSize())
});
Output of the window size
{ height: 500, width: 400 }
We can set the postion of the browser window using setWindowPosition
Use getWindowPosition()
to get window position.
it('getWindowPosition Commands with WebdriverIO', () => {
browser.url('https://chercher.tech/practice/popups')
browser.setWindowPosition(400, 500)
console.log(browser.getWindowPosition())
console.log(browser.getWindowPosition().x)
console.log(browser.getWindowPosition().y)
});
navigateTo() function takes the user to given webpage based on the url passed.
WebdriverIO provides back()
command to move backwards in the browser's history.
forward() command moves forward in a browser history.
refresh() function refreshes the current webpage in WebdriverIO
it('Navigation Commands in WebdriverIO', () => {
browser.url('https://chercher.tech/practice/popups')
browser.url("http://google.com")
browser.back()
console.log(browser.getTitle())
browser.forward()
console.log(browser.getTitle())
browser.refresh()
console.log(browser.getTitle())
});
Article is written by Pavan (a) KarthiQ. Well, I am serving notice period in an MNC, Bangalore. I thought to enrich every person knowledge a little, I always have a feeling, when we teach something, we will learn more than what you know.
Knowledge is the only thing that doubles when you spend it.
I have also created the reporter for Protractor Jasmine. Use for your projects without any hesitation