Exp 4 SY
Exp 4 SY
Test Scenario
Here, We will automate the testing for the following scenario in Google Chrome Browser by
developing Test script using Selenium Web Driver:
Driver
We need unique id or name for required web elements (Search textbox and "Google Search" button)
to automate them through your test script. It can be done as follows:
• Repeat the same for "Google Search" button. Here, unique name of "Google Search" button
is "btnK".
Code Explaination
1. Open Eclipse application,, click File -> New -> Java Project and create Java project as "ST
Lab" (Note: Eclipse IDE and JDK 8 or above must be downloaded and installed earlier.
Refer Experment 3 to know
now how to download and install).
install
2. Right click on the "src" folder and create a new class file from the New -> Class.
3. Give Class name as "Exp4"
"Exp4 and click the "Finish" buton.
4. Right click on the "src" folder and click "Build Path" ->
> "Configure Build Path".
5. Under Libraries tab select "Classpath" and click "Add External JARs". Then, add all the
required JAR files ("client
client-combined-3.141.59.jar", " client-combined--3.141.59-sources.jar",
all jar files from "libs" folder and "junit4.jar" file) and finally, click "Apply and Close".
(Note: Downloading required JAR files explained in Experiment 3. Refer Experiment 3 for
more information)
6. Import the required packages and set the system property "webdriver.chrome.driver" to
the path of your ChromeDriver.exe file (Note: Chrome Driver must be downloaded earlier.
Refer Experment 3 to know how to download chrome driver).
driver)
System.setProperty("webdriver.chrome.driver",
("webdriver.chrome.driver", "D:\\STLab\\chromedriver.exe");
chromedriver.exe");
7. Create a instance of Web driver class, launch
unch the browser and navigate it to the required
google.com. Maximize the browser if required.
URL: https://www.google
WebDriver driver=new ChromeDriver();
driver.navigate().to("https://www.
driver.navigate().to("https://www.google.com");
driver.manage().window().maximize();
8. Next, you need to locate the search textbox and "Google Search" button element. Send
value to search text box and perform click action on button element as below:
driver.findElement(By.
.findElement(By.name("q")).sendKeys("Software
q")).sendKeys("Software Testing");
Testing
driver.findElement(By.
.findElement(By.name("btnK")).submit();
9. To validate results, assertions are needed. Asserts compares expected result with actual
result. Here, Expected Title of search results web page is "Software Testing - Google
Search" on search operation.
operation It is compared with current tilte of the web page as below:
String ExpTitle = "Software
" Testing - Google Search";
Assert.assertEquals(ExpTitle driver.getTitle());
Assert.assertEquals(ExpTitle,
10. close the browser.
driver.close();
Computer Science & Engineering
SOFTWATE TESTING LAB - 15CS64P 2020-21
Test Script
import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Exp4 {
@Test
public void testGoogle()()
{
//Set system property
System.setProperty chromedriver.exe");
setProperty("webdriver.chrome.driver","D:\\STLab\\chromedriver.exe"
//Creates instance of webDtiver class
WebDriver driver=new
driver ChromeDriver();
//Launch the browser and navigate to "https://www.linkedin.com/login"
driver.navigate().to(
.navigate().to("https://www.google.com");
//Maximize the Browser
driver.manage().window().maximize();
.manage().window().maximize();
// Locate the search textbox and send value
driver.findElement(By.
.findElement(By.name("q")).sendKeys("Software
"Software Testing");
Testing"
// Locate the button and click
driver.findElement(By.
.findElement(By.name("btnK")).submit();
//Expected URL on successful login is: https://www.linkedin.com/feed/
String ExpTitle = "Software Testing - Google Search";
//Compares expected result with actual result.
Assert.assertEquals
assertEquals(ExpTitle, driver.getTitle());
System.out.println(
.println("Test Passed\nGoogle
nGoogle Search is Successful: ");
"
//Close the browser
driver.close();
}
}
Test Result:
REFERENCES
[1] https://www.selenium.dev/
[2] https://www.javatpoint.com/selenium-webdriver
https://www.javatpoint.com/selenium
[3] https://www.tutorialspoint.com/selenium/selenium_webdriver.htm
[4] https://www.google.com