Tuesday, May 12, 2020

Switch to another tab of same browser with Selenium WD using java

Test Scenario/step:

1. Open a browser say Firefox
2. Navigate to Test URL  https://link.testproject.io/bsg
3. Click Login link/button at top right corner --> It opens a new tab
4. Switch to new (2nd) tab and do whatever you need

Selenium Java code:

driver.get(" https://link.testproject.io/bsg");
driver.findElement(By.linkText("Login")).click();
//The following snippet of code is for switching to new tab
Set<String> handles = driver.getWindowHandles();
Iterator<String> it = handles.iterator();
while (it.hasNext()){
String child = it.next();
driver.switchTo().window(child);
}

Sunday, May 10, 2020

TestNG Assertion in Selenium WebDriver with Java

Test Scenario/Steps:

1. Open a browser (in my case Firefox)
2. Navigate to test URL say https://link.testproject.io/bsg
3. Assert the title of page
4. Click Login link at the top right corner
5. Enter invalid Username & Email --> Click Sign in button
6. Login will fail and a notification message will be displayed --> Assert the validation message

The following is the complete Selenium java code using TestNG framework:

package assertion_verification;

import java.util.ArrayList;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

public class ValidationOfInvalidLogin_TestNG {
private WebDriver driver;
JavascriptExecutor js;
String baseURL;
@BeforeClass
public void setUp() {
System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir") + "\\" + "src\\test\\resources\\Driver\\geckodriver-v0.24.0-win64\\geckodriver.exe");
driver = new FirefoxDriver();
js = (JavascriptExecutor) driver;
baseURL = "https://link.testproject.io/bsg";
}
@AfterClass
public void tearDown() {
driver.quit();
}

@Test
public void validationOfInvalidLogin() throws Exception{
//Open test URL: https://link.testproject.io/bsg
driver.get(baseURL);
Thread.sleep(3000);
//assertion of title of page using TestNG
String actualTitle = driver.getTitle();
String expectedTitle = "Free Test Automation For All – TestProject";
assertEquals(expectedTitle,actualTitle);
//OR,
assertTrue(actualTitle.contains(expectedTitle));

String parentWindowHandle = driver.getWindowHandle();
driver.findElement(By.linkText("Login")).click();
Thread.sleep(3000);

ArrayList<String> windowHandles = new ArrayList<String>(driver.getWindowHandles());
for (String window:windowHandles){
if (window != parentWindowHandle){
driver.switchTo().window(window);
}
}
Thread.sleep(3000);

driver.findElement(By.name("username")).clear();
driver.findElement(By.name("username")).sendKeys("fff@gmail.com");

driver.findElement(By.id("password")).clear();
driver.findElement(By.id("password")).sendKeys("1234567890");
//Click Signin button
driver.findElement(By.id("tp-sign-in")).click();
//Assertion of notification message with TestNG
String actualMsg = driver.findElement(By.id("tp-message-error")).getText();
String expectedMsg = "Invalid username or password.";
assertEquals(expectedMsg,actualMsg);
}
}

Wednesday, May 6, 2020

Getting started with Selenium IDE

Selenium IDE is the starting for new to Selenium. It's a Chrome and Firefox extension.

Prerequisite: To install Selenium IDE in Firefox and Google Chrome.

Installation in FF:

1. Launch Firefox and navigate to https://addons.mozilla.org/en-US/firefox/addon/selenium-ide/
2. Click on "+ Add to Firefox" link/button
3. Click Add button from permission prompt
4. Click "OK, Got it" from the dialogue at top right corner
5. Selenium icon will be displayed at the right side of address bar of browser after successful installation

Installation in Chrome:

1. Launch Google Chrome and navigate to https://chrome.google.com/webstore/detail/selenium-ide/mooikfkahbdckldjjndioackbalphokd?hl=en
2. Click "Add to Chrome" link/button
3. Click "Add Extension" button from permission prompt
4. Selenium icon will be displayed at the right side of address bar of browser after successful installation

It's the time to get started--->
1. Open the browser say Firefox -> Click the Selenium icon at top right corner at address bar
2. Click "Create a new project" from Welcome dialogue of Selenium IDE
3. Enter Project name say "FirstTest" --> Click OK
4. Click Recording icon (REC) at the top right corner
5. Enter Test URL
6. Click START RECORDING button --> Your site will open to browse
7. Click some button/link, enter text in text box
8. Observe Selenium IDE --> It will record your test steps you did in step 7 having Command, Target and Value
9. Click Stop Recording icon (Red square inside red circle) at the top right corner and Save the project as .side extension say FirstTest.side
10. Replay the recorded test