Writing Integration Tests with Selenium & Jest
For writing test cases with Selenium & Jest, we need to make sure that the browser window is correctly initialized before any tests. We can use the beforeAll
block to ensure that the browser window is initialized before the tests start executing:
js99123456789101112131415161718let driver = null;beforeAll(async () => {let chromeDriverPath = path.resolve('C:/Users/AIMS TECH/Desktop/twitter-like-application-3/chromedriver.exe');let service = new chrome.ServiceBuilder(chromeDriverPath);let options = new chrome.Options();options.addArguments('--disable-search-engine-choice-screen');options.addArguments('--lang=en-GB');driver = await new Builder().forBrowser('chrome').setChromeService(service).setChromeOptions(options).build();});
It's important to define the driver
variable outside of this block to ensure that we can access it from the test blocks.
Once the test cases are done executing, we need to make sure the browser window is closed:
js9123afterAll(() => {driver.quit();});
The remainder of the process is very similar to how we would write any other kind of tests, except in this case it would involve the use of methods that control the browser to perform tests. For-example, the following test case registers a new user on our twitter application:
js99123456789101112131415161718192021222324252627282930313233test('users can successfully register', async () => {// 1. Go to the login pagedriver.get('http://localhost:3000');// 2. Click "Register here" hyperlinklet registerLink = await driver.findElement(By.id('register-link'));registerLink.click();// 3. Wait till Registration page is loadedawait driver.wait(until.elementLocated(By.xpath('/html/body/div/h1')));let h1Element = driver.findElement(By.xpath('/html/body/div/h1'));await driver.wait(until.elementTextIs(h1Element, 'Register'));// 4. Enter "testuser1" in username fieldlet usernameField = await driver.findElement(By.id('username-input'));usernameField.sendKeys("testuser1");// 5. Enter "examplePassword123" in password fieldlet passwordField = await driver.findElement(By.id('password-input'));passwordField.sendKeys("examplePassword123");// 6. Click "Register"let registerBtn = await driver.findElement(By.id('register-btn'));registerBtn.click();// 7. Verify that the user is redirected to the Login pageawait driver.wait(until.elementLocated(By.xpath('/html/body/div/h1')));h1Element = driver.findElement(By.xpath('/html/body/div/h1'));expect(await h1Element.getText()).toBe("Login");}, 10000);
In the above code, the timeout value for this test case is manually set to 10000
or 10 seconds because Selenium test cases can potentially take longer than the default 5000
(5 seconds) timeout.
It is important to note that the test cases must contain at least one assertion, also known as an expect
statement. The success or failure of the test case are decided by the results of the assertions.
Following is the full code of the test script used in the video:
js9912345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697const { Builder, By, until } = require('selenium-webdriver');const chrome = require('selenium-webdriver/chrome');const path = require('path');let driver = null;beforeAll(async () => {let chromeDriverPath = path.resolve('C:/Users/AIMS TECH/Desktop/twitter-like-application-3/chromedriver.exe');let service = new chrome.ServiceBuilder(chromeDriverPath);let options = new chrome.Options();options.addArguments('--disable-search-engine-choice-screen');options.addArguments('--lang=en-GB');driver = await new Builder().forBrowser('chrome').setChromeService(service).setChromeOptions(options).build();});afterAll(() => {driver.quit();});const sleep = (milliseconds) => {return new Promise(resolve => setTimeout(resolve, milliseconds));};describe('authentication tests', () => {describe('registration system', () => {test('users can successfully register', async () => {// 1. Go to the login pagedriver.get('http://localhost:3000');// 2. Click "Register here" hyperlinklet registerLink = await driver.findElement(By.id('register-link'));registerLink.click();// 3. Wait till Registration page is loadedawait driver.wait(until.elementLocated(By.xpath('/html/body/div/h1')));let h1Element = driver.findElement(By.xpath('/html/body/div/h1'));await driver.wait(until.elementTextIs(h1Element, 'Register'));// 4. Enter "testuser1" in username fieldlet usernameField = await driver.findElement(By.id('username-input'));usernameField.sendKeys("testuser1");// 5. Enter "examplePassword123" in password fieldlet passwordField = await driver.findElement(By.id('password-input'));passwordField.sendKeys("examplePassword123");// 6. Click "Register"let registerBtn = await driver.findElement(By.id('register-btn'));registerBtn.click();// 7. Verify that the user is redirected to the Login pageawait driver.wait(until.elementLocated(By.xpath('/html/body/div/h1')));h1Element = driver.findElement(By.xpath('/html/body/div/h1'));expect(await h1Element.getText()).toBe("Login");}, 10000);});describe('login system', () => {test('users can successfully login with correct credentials', async () => {// 1. Go to the login pagedriver.get('http://localhost:3000');// 2. Enter "testuser1" in the username fieldlet usernameField = await driver.findElement(By.id('username-input'));usernameField.sendKeys('testuser1');// 3. Enter "examplePassword123" in the password fieldlet passwordField = await driver.findElement(By.id('password-input'));passwordField.sendKeys('examplePassword123');// 4. Click "Login"let loginBtn = await driver.findElement(By.xpath('/html/body/div/form/button'));loginBtn.click();// 5. Verify that the application takes the user to the welcome page (also known as the dashboard)await driver.wait(until.elementLocated(By.xpath('/html/body/div/h1')));let h1Element = driver.findElement(By.xpath('/html/body/div/h1'));let h1Text = await h1Element.getText();expect(h1Text).toBe('Welcome, testuser1');});}, 10000);});
Thanks for your feedback!
Ask AI
Ask anything or try one of the suggested questions to begin our chat