DataInputProvider.java
public class DataInputProvider {
public static ArrayList<ArrayList<String>> getSheet(String dataSheetName, String SheetName) throws IOException{
ArrayList<ArrayList<String>> data = new ArrayList<ArrayList<String>>();
try {
FileInputStream fis = new FileInputStream(new File(System.getProperty("user.dir")+"\\data\\"+dataSheetName+".xlsx"));
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet sheet = workbook.getSheet(SheetName);
// get the number of rows
int rowCount = sheet.getLastRowNum();
// get the number of columns
int columnCount = sheet.getRow(0).getLastCellNum();
// loop through the rows
for(int i=1; i <rowCount+1; i++){
try {
XSSFRow row = sheet.getRow(i);
ArrayList<String> record = new ArrayList<String>();
for(int j=0; j <columnCount; j++){ // loop through the columns
try {
record.add(row.getCell(j).getStringCellValue()); // add to the record
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
data.add(record);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// workbook.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return data;
}
}
ExcelReporter.Java
public class ExcelReporter {
private static XSSFWorkbook workbook = null;
private static XSSFSheet sheet = null;
private static XSSFRow row = null;
/*
* this method creates the header for reporter
*/
static void createReportHeader(String testcaseSheetName) {
workbook = new XSSFWorkbook();
sheet = workbook.createSheet(testcaseSheetName);
row = sheet.createRow(0);
// row.createCell(0).setCellValue("SNo");
row.createCell(0).setCellValue("Step No");
row.createCell(1).setCellValue("Description");
row.createCell(2).setCellValue("Status");
}
static void flushWorkbook(String testCaseName) throws IOException {
FileOutputStream fos = new FileOutputStream(new File(System.getProperty("user.dir")+"\\report\\"+testCaseName+".xlsx"));
workbook.write(fos);
}
static void reportStep(String desc, String status) throws IOException {
// ArrayList<ArrayList<String>> data1 = DataInputProvider.getSheet("TestSteps","Sheet1");
row = sheet.createRow(sheet.getLastRowNum()+1);
row.createCell(0).setCellValue(sheet.getLastRowNum());
row.createCell(1).setCellValue(desc);
row.createCell(2).setCellValue(status);
if(status.toUpperCase().equals("SUCCESS"))
//ATUReports.add("Pass Step 1", LogAs.PASSED, new CaptureScreen(ScreenshotOf.DESKTOP));
ATUReports.add("Enter the URL", "saravanan", "Gnanam", "FiO", LogAs.PASSED, new CaptureScreen(ScreenshotOf.BROWSER_PAGE));
else
ATUReports.add("Enter the URL", "saravanan", "Gnanam", "FiO", LogAs.FAILED, new CaptureScreen(ScreenshotOf.DESKTOP));
}
}
Wrappermethods.java
public class WrapperMethods {
WebDriver driver;
int i=0;
private String testCaseName;
private int dataSet;
public WrapperMethods(String testCaseName, int dataSet){
this.testCaseName = testCaseName;
this.dataSet = dataSet;
System.setProperty("atu.reporter.config", "atu.properties");
}
public WebDriver invokeApplication(String browser, String url,String desc, String expResult, String actResult) throws IOException{
ExcelReporter.createReportHeader(testCaseName);
try {
if(browser.toLowerCase().equals("ie")){
System.setProperty("webdriver.ie.driver", "..\\drivers\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
}
else if(browser.toLowerCase().equals("chrome")){
System.setProperty("webdriver.chrome.driver", "..\\drivers\\ChromeDriver.exe");
driver = new ChromeDriver();
}
else{
driver = new FirefoxDriver();
}
ATUReports.setWebDriver(driver);
ATUReports.indexPageDescription = "TestLeaf Demo Project";
driver.get(url);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
TC001_Login.reportStep(desc,url,"SUCCESS", expResult, actResult);
} catch (WebDriverException exception) {
TC001_Login.reportStep(desc,url,"FAILURE",expResult, actResult);
}
return driver;
}
public void enterValueById(String id, String value,String desc, String expResult, String actResult) throws IOException{
try {
driver.findElement(By.id(id)).clear();
WebElement element=driver.findElement(By.id(id));
element.sendKeys(value);
// ATUReports.add("Enter the URL", "saravanan", "Gnanam", "FiO", LogAs.PASSED, new CaptureScreen(element));
TC001_Login.reportStep(desc,value,"SUCCESS", expResult, actResult);
} catch (NoSuchElementException exc) {
TC001_Login.reportStep(desc,value,"FAILURE",expResult, actResult);
} catch (WebDriverException e) {
TC001_Login.reportStep(desc,value,"FAILURE",expResult, actResult);
} finally{
takeSnapshot();
}
}
public void enterValueByName(String name, String value,String desc, String expResult, String actResult) throws IOException{
try {
driver.findElement(By.name(name)).clear();
driver.findElement(By.name(name)).sendKeys(value);
TC001_Login.reportStep(desc,value,"SUCCESS",expResult, actResult);
} catch (NoSuchElementException exc) {
TC001_Login.reportStep(desc,value,"FAILURE",expResult, actResult);
} catch (WebDriverException exception){
TC001_Login.reportStep(desc,value,"FAILURE",expResult, actResult);
} finally{
takeSnapshot();
}
}
public void selectDropdownValueById(String id, int index) throws IOException{
try {
WebElement element = driver.findElement(By.id(id));
Select dropDownElement = new Select(element);
dropDownElement.selectByIndex(index);
ExcelReporter.reportStep("Element with id :"+id+" is found and index :"+index+" selected successfully..","SUCCESS");
} catch (NoSuchElementException exception) {
ExcelReporter.reportStep("Element with id :"+id+"could not found..","FAILURE");
} catch (WebDriverException e){
ExcelReporter.reportStep("Driver could not found !!!","FAILURE");
} finally{
takeSnapshot();
}
}
public void linkClickByText(String text,String desc, String expResult, String actResult) throws IOException{
try {
driver.findElement(By.linkText(text)).click();
TC001_Login.reportStep(desc,null,"SUCCESS", expResult, actResult);
} catch (NoSuchElementException e) {
TC001_Login.reportStep(desc,null,"FAILURE", expResult, actResult);
} catch (WebDriverException exe){
TC001_Login.reportStep(desc,null,"FAILURE", expResult, actResult);
} finally{
takeSnapshot();
}
}
public void clickByCSS(String css,String desc, String expResult, String actResult) throws IOException{
try {
driver.findElement(By.cssSelector(css)).click();
TC001_Login.reportStep(desc,null,"SUCCESS", expResult, actResult);
} catch (NoSuchElementException e) {
TC001_Login.reportStep(desc,null,"FAILURE", expResult, actResult);
} catch (WebDriverException exe){
TC001_Login.reportStep(desc,null,"FAILURE", expResult, actResult);
} finally{
takeSnapshot();
}
}
public void closeApplication(String desc, String expResult, String actResult) throws IOException{
try {
driver.quit();
TC001_Login.reportStep(desc,null,"SUCCESS",expResult, actResult);
} catch (WebDriverException exe){
TC001_Login.reportStep(desc,null,"FAILURE",expResult, actResult);
}
ExcelReporter.flushWorkbook(testCaseName+"-Run"+dataSet);
}
public void takeSnapshot() throws IOException{
try {
File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(src, new File("..\\reports\\snaps\\snapshot"+ i +".png"));
i++;
} catch (IOException ioe) {
// TODO Auto-generated catch block
ExcelReporter.reportStep("Unable to copy the file !!!!","FAILURE");
}
}
}
TC001_login.java
@Listeners({ ATUReportsListener.class, ConfigurationListener.class, MethodListener.class }) public class TC001_Login {
Properties prop = new Properties();
InputStream input = null;
WebDriver driver;
String desc,inputt, expResult, actResult;
{
System.setProperty("atu.reporter.config", "C:\\Users\\Shanmugam\\workspace\\testProject\\atu.properties");
}
@DataProvider(name = "test")
public Object[][] createData1() throws IOException {
Object[][] object=new Object[2][2];
ArrayList<ArrayList<String>> data = DataInputProvider.getSheet("Login","Sheet1");
for (int i = 0; i < data.size(); i++) {
object[i][0]=(Object)data.get(i).get(0);
object[i][1]=(Object)data.get(i).get(1);
}
return object;
}
@SuppressWarnings("deprecation")
@Test(dataProvider = "test")
public void loginMhs(String n1, String n2) throws IOException {
ArrayList<ArrayList<String>> data1 = DataInputProvider.getSheet("Login","Sheet2");
// for (int i = 1; i < data1.size(); i++) {
int i=0;
WrapperMethods wM = new WrapperMethods("MHS Login",0);
// launch the browser
wM.invokeApplication("firefox", "http://ift.tt/1xrkH9T",data1.get(i).get(0),data1.get(i).get(1), data1.get(i).get(2));
try {
input = new FileInputStream("C:\\Users\\Shanmugam\\workspace\\testProject\\object.properties");
// load a properties file
prop.load(input);
i++;
// enter user name
wM.enterValueById(prop.getProperty("username"), n1,data1.get(i).get(0),data1.get(i).get(1), data1.get(i).get(2));
i++;
// enter password
wM.enterValueByName(prop.getProperty("password"),n2,data1.get(i).get(0),data1.get(i).get(1), data1.get(i).get(2));
i++;
// click login
wM.clickByCSS(prop.getProperty("loginSubmit"),data1.get(i).get(0),data1.get(i).get(1), data1.get(i).get(2));
i++;
// click logout
wM.linkClickByText(prop.getProperty("logoff"),data1.get(i).get(0),data1.get(i).get(1), data1.get(i).get(2));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
i++;
// close app
wM.closeApplication(data1.get(i).get(0),data1.get(i).get(1), data1.get(i).get(2));
}
public static void reportStep(String desc, String inputt, String status, String expResult, String actResult) throws IOException {
// ArrayList<ArrayList<String>> data1 = DataInputProvider.getSheet("TestSteps","Sheet1");
if(status.toUpperCase().equals("SUCCESS"))
//ATUReports.add("Pass Step 1", LogAs.PASSED, new CaptureScreen(ScreenshotOf.DESKTOP));
ATUReports.add(desc,inputt,expResult,actResult, LogAs.PASSED, new CaptureScreen(ScreenshotOf.BROWSER_PAGE));
else
ATUReports.add(desc,inputt,expResult,actResult, LogAs.FAILED, new CaptureScreen(ScreenshotOf.DESKTOP));
}
}
Atu.properties
atu.reports.dir=C:\Users\Shanmugam\workspace\testProject\ATU Reports
atu.proj.header.text=ATUReport Testing
atu.proj.header.logo=C:\Users\Shanmugam\workspace\testProject\ATU Reports\HTML_DESIGN_Files\IMG\atu.png
atu.proj.description=MyProject Testing Reports
atu.reports.takescreenshot=true
atu.reports.configurationreports=false
atu.reports.excel=false
atu.reports.continueExecutionAfterStepFailed = true
atu.reports.recordExecution=suite
atu.reports.setMaxRuns = 100
atu.reports.pdf=false
object.properties
Object list for Login script
Enter the Username
username=land_username
Enter the Password
password=txtPassword
Login link object
loginSubmit=input[type='image']
Log off browser
logoff=Logout
Login.xlsx Sheet2 Step Desc Expected Result Actual result Open the browser Browser should open with desired input value Works as expected for URL Enter the Username Element for username found and entered the value Works as expected for Username Enter the Password Element for password found and entered the value Works as expected for Password Submit the login Login to the app Works as expected for submit option Log out the User Logout from existing user Works as expected Close the application Close the app Works as expected for browser close
Aucun commentaire:
Enregistrer un commentaire