So I've recently started making a few web user interface tests that can't be manually achieved with API calls. I was looking forward to practising my Node JS so thought I'd make it using that framework.
Here's a few little functions and commands to both get you going or help me remember what I've done:
Firstly, Selenium is a web browser automation software that can be run on the browser, in an application such as node or as an IDE. find out more HERE
Secondly, Node JS my desired programming service of the day is an open source platform built on a javascript runtime. There's a great youtube introductory video HERE
So... Getting Started. So I had installed node and created a dedicated directory.
I navigated to my new directory and installed selenium-webdriver:
From here on there are a whole bunch of commands we can run using driver.Command you can see a list of some of the commands HERE.
The next thing to do is to perform a GET function on the webpage you want to navigate to in your automated test.
Populate an object:
Click:
Here's a few little functions and commands to both get you going or help me remember what I've done:
Firstly, Selenium is a web browser automation software that can be run on the browser, in an application such as node or as an IDE. find out more HERE
Secondly, Node JS my desired programming service of the day is an open source platform built on a javascript runtime. There's a great youtube introductory video HERE
So... Getting Started. So I had installed node and created a dedicated directory.
I navigated to my new directory and installed selenium-webdriver:
npm install selenium-webdriverI then created an application:
echo "" > myNewApp.jsIn the application I referenced selenium-webdriver and then used selenium builder to create a new browser driver like so:
You'll either want different variable names for your different browsers or to only call one at a time!const selenium = require('selenium-webdriver'),By = selenium.By,until = selenium.until;var driver = new selenium.Builder().forBrowser('chrome').build();var driver = new selenium.Builder().forBrowser(‘firefox’).build();
From here on there are a whole bunch of commands we can run using driver.Command you can see a list of some of the commands HERE.
The next thing to do is to perform a GET function on the webpage you want to navigate to in your automated test.
From this page we can perform a whole bunch of commands:driver.get('www.myUrl.com').then(function(){console.log('Navigating to my page');
});
Populate an object:
driver.findElement(By.name(‘red’)).sendKeys(‘ABC’);Clear a populated object:
driver.findElement(By.name(‘red’)).clear();Make a hidden object visible:
driver.executeScript("document.getElementsByName(‘red’)[0].setAttribute('type', 'text');");Using the drivers .then function will let you wait for something to occur and then perform the next action, such as wait for 2 seconds:
You might want to click a button, all you need to know is the button class name. One way of doing this is to "inspect" the webpage from the browser..then(function(){driver.sleep(2000);});
Click:
driver.findElement(By.className('btn btn-lg')).click();To quit the browser:
driver.quit();The one thing I wasn't able to do was make a webpage stop loading to allow me to process a page that didn't stop. Let me know if you've done this before! I'd love to hear how you did it.