use selenium webdriver to open google.com and verify that the search bar accepts text in javascript

To use Selenium WebDriver in JavaScript to open google.com and verify that the search bar accepts text, you can follow these steps:

  1. Install Selenium WebDriver using npm:
index.tsx
npm install selenium-webdriver
31 chars
2 lines
  1. Create a new file and import the necessary libraries:
index.tsx
const {Builder, Key, By, until} = require('selenium-webdriver');
65 chars
2 lines
  1. Create a new WebDriver instance and navigate to google.com:
index.tsx
let driver = new Builder()
    .forBrowser('chrome')
    .build();

driver.get('https://www.google.com/');
107 chars
6 lines
  1. Find the search bar element and enter text into it:
index.tsx
let searchBox = driver.findElement(By.name('q'));
searchBox.sendKeys('Hello World', Key.RETURN);
97 chars
3 lines
  1. Verify that the search results page has loaded:
index.tsx
driver.wait(until.titleContains('Hello World'), 1000)
    .then(() => console.log('Page title contains "Hello World"'));
121 chars
3 lines
  1. Close the browser window:
index.tsx
driver.quit();
15 chars
2 lines

The full code will look like this:

index.tsx
const {Builder, Key, By, until} = require('selenium-webdriver');

let driver = new Builder()
    .forBrowser('chrome')
    .build();

driver.get('https://www.google.com/');
let searchBox = driver.findElement(By.name('q'));
searchBox.sendKeys('Hello World', Key.RETURN);
driver.wait(until.titleContains('Hello World'), 1000)
    .then(() => console.log('Page title contains "Hello World"'));
driver.quit();
406 chars
13 lines

This code will open a new Chrome window, navigate to google.com, enter "Hello World" into the search bar, and verify that the search results page has loaded. It will then print a message to the console and close the browser window.

gistlibby LogSnag