30 – Writing Selenium Test Cases

Selenium Class 30 – Writing Selenium Test Cases

Prerequisites for writing Selenium WebDriver Test Cases…

i) Test Scenario or Manual Test Case
ii) Element Locators to Locate/Recognize/Identify Elements/Objects
iii) Selenium WebDriver API Commands to perform operations on elements
iv) Programming Concepts to enhance the Test Cases
v) Testing Framework (Junit/TestNG) to group Test cases, prioritize Test cases, execute Test batches and generate Test Reports
…………………………………..
Write Test Cases…

Test Case 1: Verify Internal and External Links……
Test Case 2: Verify the Element Existence…
Test Case 3: Verify Customer Registration…..

Test Case 1: Verify Internal and External Links in wikipedia.org

Test Steps:
i) Launch the Browser
ii) Navigate to wikipedia.org selenium page
iii) Click “create account” link
iv) Capture the URL
v) Navigate back to Selenium Page
vi) Click “seleniumhq.org” link
vii) Capture the URL
viii) Close the Browser

Verification Point/s:
i) Verify if the 1st URL is an Internal link
ii) Verify if the 2nd URL is an External link

Input Data/Test Data:
NA

Expected Result/URL:
1) https://en.wikipedia.org/w/index.php?title=Special:CreateAccount&returnto=Selenium+%28software%29
2) http://www.seleniumhq.org

Selenium WebDriver Test Steps:

System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
WebDriver driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get(“https://en.wikipedia.org/wiki/Selenium_(software)#Selenium_WebDriver”);
driver.findElement(By.linkText(“Create account”)).click();
String url1=driver.getCurrentUrl();
//System.out.println(url1);

if (url1.contains(“wikipedia.org”)){
System.out.println(“1st Verification: “+ “It is an Internal Link – Passed”);
}
else{
System.out.println(“1st Verification: “+ “It is Not an Internal Link – Failed”);
}
driver.navigate().back();
//Thread.sleep(4000);
driver.findElement(By.partialLinkText(“seleniumhq.org”)).click();
//Thread.sleep(4000);
String url2=driver.getCurrentUrl();
//System.out.println(url2);
if (url2.equals(“http://www.seleniumhq.org/”)){
System.out.println(“2nd Verification: “+ “It is an External Link – Passed”);
}
else
{
System.out.println(“2nd Verification: “+ “It is Not an External Link – Failed”);
}
driver.close();
}
}

Negative Test Case:

System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
WebDriver driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get(“https://en.wikipedia.org/wiki/Selenium_(software)#Selenium_WebDriver”);
driver.findElement(By.linkText(“Create account”)).click();
String url1=driver.getCurrentUrl();
//System.out.println(url1);

if (url1.contains(“abcdef.org”)){
System.out.println(“1st Verification: “+ “It is an Internal Link – Passed”);
}
else{
System.out.println(“1st Verification: “+ “It is Not an Internal Link – Failed”);
}
driver.navigate().back();
//Thread.sleep(4000);
driver.findElement(By.partialLinkText(“seleniumhq.org”)).click();
//Thread.sleep(4000);
String url2=driver.getCurrentUrl();
//System.out.println(url2);
if (url2.equals(“http://www.seleniumhq1.org/”)){
System.out.println(“2nd Verification: “+ “It is an External Link – Passed”);
}
else
{
System.out.println(“2nd Verification: “+ “It is Not an External Link – Failed”);
}
driver.close();
}
}

Note:
i) We use valid input for Positive Testing and invalid input for Negative Testing
ii) If there is no input data in our Test Case then use incorrect expected Result for Negative Testing

Test Case 2: Verify the Element Existence…

Test Case: Verify “Gmail” Link existence in Google Home page

Test Steps:
i) Launch the Browser
ii) Navigate to Google Home page (“https://www.google.co.in”)

Verification Point/s:
Verify the existence of “Gmail” Link

Input Data:
NA

Selenium WebDriver Test Case:

System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
WebDriver driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get(“https://www.google.com”);

boolean linkExistence=driver.findElement(By.linkText(“Gmail”)).isDisplayed();

if (linkExistence == true){
System.out.println(“Gmail Link Exists – Passed”);
}
else{
System.out.println(“Gmail Link Not Exists – Failed”);
}
driver.close();
…………………………………..
System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
WebDriver driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get(“https://www.google.com”);

if (driver.findElement(By.linkText(“Gmail”)).isDisplayed()== true){
System.out.println(“Gmail Link Exists – Passed”);
}
else{
System.out.println(“Gmail Link Not Exists – Failed”);
}
driver.close();
…………………………………..
Negative Testing…

System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
WebDriver driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get(“https://www.google.com”);
try{
boolean linkExistence=driver.findElement(By.linkText(“abcd”)).isDisplayed();

if (linkExistence == true){
System.out.println(“Gmail Link Exists – Passed”);
}
}
catch(NoSuchElementException e){
System.out.println(“Gmail Link Not Exists – Failed”);
}
driver.close();


Selenium Step by Step Tutorial

Test Case 3: Customer Registration in gcrShop web portal User Interface

Test Steps:
i) Lunch the Browser
ii) Navigate to gcrShop User Interface (http://www.gcrit.com/build3/)
iii) Click “create an account” link
iv) Enter all mandatory fields
v) Click “Continue” Button

Verification Point/s
Capture the confirmation message after submission and compare with expected result

Expected Result:
“Your Account Has Been Created!”
…………………………………..
Selenium WebDriver Test Script/Test Case:

System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();

driver.get(“http://www.gcrit.com/build3/”);
driver.findElement(By.linkText(“create an account”)).click();
driver.findElement(By.xpath(“//*[@id=’bodyContent’]/form/div/div[2]/table/tbody/tr[1]/td[2]/input[2]”)).click();
driver.findElement(By.name(“firstname”)).sendKeys(“abcd”);
driver.findElement(By.name(“lastname”)).sendKeys(“xyz”);
driver.findElement(By.id(“dob”)).sendKeys(“10/10/1990”);
driver.findElement(By.name(“email_address”)).sendKeys(“divyapriya345a@gmail.com”);

driver.findElement(By.name(“street_address”)).sendKeys(“asd fgh”);
driver.findElement(By.name(“postcode”)).sendKeys(“12345”);
driver.findElement(By.name(“city”)).sendKeys(“Hyderabad”);
driver.findElement(By.name(“state”)).sendKeys(“Telangana”);

Select dropdown = new Select (driver.findElement(By.name(“country”)));
dropdown.selectByVisibleText(“India”);

driver.findElement(By.name(“telephone”)).sendKeys(“9878787898”);
driver.findElement(By.name(“password”)).sendKeys(“abcd123”);
driver.findElement(By.name(“confirmation”)).sendKeys(“abcd123”);

driver.findElement(By.id(“tdb4”)).click();

String message = driver.findElement(By.tagName(“h1”)).getText();
//System.out.println(message);

if (message.equals(“Your Account Has Been Created!”)){
System.out.println(“Customer Registration is Successful – Passed”);
}
else
{
System.out.println(“Customer Rigistartion is Unsuccessful – Failed”);
}
driver.close();