When to choose WebdriverJS over selenium Java?
Before starting our first example program, we need to install WebdriverJs. Click here to go through step by step installation process.
Now, let’s start writing a basic example program.
2. Write Program
Create the below test script (JS) file in your project folder.
var webdriver = require('selenium-webdriver'), By = webdriver.By, until = webdriver.until; var driver = new webdriver.Builder() .forBrowser('chrome') .build(); driver.get('http://www.webdriverjs.com/'); driver.findElement(By.css('.search-field.form-control')).sendKeys("WebdriverJs"); driver.findElement(By.css('button.search-submit i')).click(); driver.wait(until.elementTextIs(driver.findElement(By.css('.vl-main-header>h1')), "Search Results for: WebdriverJs"), 10000); driver.quit();
We can configure the driver using
webdriver.builder(). We can use all the element identification strategies like Xpath, cssSelector, className, id etc..
driver.wait( is an explicit wait like in selenium.
3. Add driver in Path
This is an additional step specific to webdriverjs which you don’t find in other selenium based automation tools.
WebdriverJs will try to use the browser driver (ChromeDriver/geckodriver/safaridriver etc..) from system path variable. So we need to add the driver location to the path variable.
Please follow the below steps to add driver to your PATH variable.
- First download the required version of the driver. You can find download locations of drivers Here.
- Extract the driver from downloaded to zip file and save it in your required location.
IMPORTANT: Remove version information from the driver file name. If your extracted driver name is ‘chromedriver_2.25.exe’ then you need to rename it to ‘chromedriver.exe’
- Now, add your driver location to the Path. Lets say your driver location is ‘Documents/Seleniumdrivers’.
- In Mac
- In Windows 10 and Windows 8
- You can set path in windows under Environment Variables -> System variables
- In Mac
4. Run Test
You would see chrome browser opens up and navigates to webdriverjs.com and searches the site with ‘WebdriverJs’ keyword and verifies the search page heading.