Jest is a JavaScript Testing Framework with a focus on simplicity. Our main focus is using Jest with Selenium.
Selenium is a great tool to automate our functional tests on websites and web applications in our favorite language. With CrossBrowserTesting, you can use Selenium and JavaScript to run automated browser tests on thousands of real mobile and desktop browsers in the cloud. To learn more about the Selenium API, we recommend you read Selenium’s documentation.
Get set up
- Install Yarn
First, we will need to install Yarn package manager:
npm install yarn - Install Jest
Then let's get Jest installed:
yarn add --dev jest - Setup Jest as package “test” script
Edit the file package.json to include the following:
"scripts": {
"test": "jest"
}
Write tests
Create file cbt.test.js
Now, just copy the following script into a text editor of your choice, and be sure to add your CBT username and authkey to the script. To get yours, sign up for a free trial or purchase a plan.
cbt.test.js
var webdriver = require("selenium-webdriver");
var cbtHub = "http://hub.crossbrowsertesting.com:80/wd/hub";
var username ='YOUR_USERNAME'; //replace with your email address
var authkey = 'YOUR_AUTHKEY'; //replace with your authkey
//set capabilities
var caps = {
name : 'Login Example',
build : '1.0',
version : '70',
platform : 'Windows 10',
screen_resolution : '1366x768',
record_video : 'true',
record_network : 'false',
browserName : 'Chrome',
username : username,
password : authkey
};
const { By, until } = webdriver
describe('webdriver', () => {
let driver;
beforeAll(async () => {
driver = new webdriver.Builder()
.usingServer(cbtHub)
.withCapabilities(caps)
.build();
await driver.get('http://crossbrowsertesting.github.io/login-form.html');
},10000);
afterAll(async () => {
await driver.quit();
}, 10000);
// test case
test('Successful Login', async () => {
await driver.findElement(webdriver.By.id("username")).sendKeys("[email protected]");
await driver.findElement(webdriver.By.xpath("//*[@type=\"password\"]")).sendKeys("test123");
await driver.findElement(webdriver.By.css("button[type=submit]")).click();
output = await driver.wait(webdriver.until.elementLocated(webdriver.By.id("logged-in")), 10000);
outputVal = await output.getAttribute('innerHTML');
expect(outputVal).toEqual('You are now logged in!');
});
});
As you can probably make out from our example test, we check if a successful login was achieved.
To run your tests simply call:
For examples and source code to this tutorial, check out our Jest GitHub Repository
If you have any trouble, feel free to get in touch. We are always happy to help!
See Also
Test Frameworks and Tools
About Selenium Testing
Selenium and JavaScript
CucumberJS
InternJS
Jasmine
Karma
Mocha
NightwatchJS
Protractor
WebDriverIO and CrossBrowserTesting