How to execute a test case with multiple sets user inputs using using the Robot objects.The beauty of Selenium Web Driver Automation provides two types. They are:
1.From Excel Sheet
2.@DataproviderAnnotation
Both of them are having it's own Pro's and Con's. I have provided the Sample code by using @Dataprovider Annotation .I will describe Writing and retrieving data from the Excel sheet in my next Post.
package TestNG;
import java.awt.AWTException;
import java.awt.Robot;
import java.util.concurrent.TimeUnit;
import junit.framework.AssertionFailedError;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class LoginTestNg {
private WebDriver driver;
private String baseUrl;
private Robot robot;
@BeforeMethod
public void setUp() throws Exception {
driver =new FirefoxDriver();
baseUrl = "https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=http://mail.google.com/mail/&scc=1<mpl=default<mplcache=2&emr=1";
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
System.out.println("@BeforeMethod - setUp");
}
////naming the inputs which we can describe by Dataprovider Annotation
@DataProvider(name = "DP1")
public Object[][] createData() {
Object[][] retObjArr={{"abcdefgh","acdfe45l@"}, ///providing the inputs in which username and password required to login.
{" ","jjjfmfmldfi"},
{"abishek","jksdhkhsdd"},
{" "," "},
{"abhishekmucherla5",""}};
return(retObjArr);
}
/////////////naming the Dataprovider annotation in @test annotation
@Test (dataProvider = "DP1")
public void logiin(String username, String password) throws Exception{
driver.get(baseUrl + "/");
driver.findElement(By.id("Email")).sendKeys(username);
driver.findElement(By.id("Passwd")).sendKeys(password);
try {
robot = new Robot();
robot.keyPress(java.awt.event.KeyEvent.VK_ENTER);
} catch (AWTException e) {
assert(e.getMessage().isEmpty()): AssertionFailedError.class.desiredAssertionStatus();
}
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@AfterMethod
public void tearDown() throws Exception {
driver.quit();
System.out.println("@AfterMethod - tearDown");
}
To know FAQ's on Selenium Please ClickHere
To know SQL's Queries Required For Testers Please ClickHere