package muttu.test;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class DataProviderDemo {
@DataProvider(name = "LoginDeatls")
public Object[][] getUserDetails() {
return new Object[][] { { "uName1", "pwd1" }, { "uName2", "pwd2" } };
}
@Test(dataProvider = "LoginDeatls")
public void Login(String name, String password) {
System.out.println(name);
System.out.println(password);
}
}
Showing posts with label selenium. Show all posts
Showing posts with label selenium. Show all posts
Tuesday, March 12, 2019
Data provider in TestNG
Labels:
annotations,
coding,
core java,
dataprovider,
development,
java.testng,
selenium,
SeleniumWedriver,
web driver,
WebDriver
Run failed test cases in Java
Create a class that fails test case
package testNG;
import org.testng.Assert;
import org.testng.annotations.Test;
public class TestFailClass {
@Test(retryAnalyzer=MyRetry.class)
public void failMethod(){
System.out.println("FAILED CLASS");
Assert.assertTrue(false);
}
}
Create a class that re-runs the failed test cases
package testNG;
import java.util.ArrayList;
import java.util.List;
import org.testng.TestNG;
public class RunFailed {
public static void main(String[] args) {
TestNG testRunner = new TestNG();
List<String> failedXmls = new ArrayList<String>();
//Enter the full path for the 'test-faailed.xml' file
failedXmls.add("C:/Users/Mutturaj.h/workspace/hulagabal/test-output/testng-failed.xml");
testRunner.setTestSuites(failedXmls);
testRunner.run();
}
}
Retry failed test cases TestNG in Java
Create test cases which fails
package testNG;
import org.testng.Assert;
import org.testng.annotations.Test;
public class TestFailClass {
@Test(retryAnalyzer=MyRetry.class)
public void failMethod(){
Assert.fail("Test failed");
}
}
Create a retry class which implements IRetryAnalyzer interface like below which tries for 3 times
package testNG;
import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;
public class MyRetry implements IRetryAnalyzer {
private int retryCount = 0;
private static final int maxRetryCount = 3;
public boolean retry(ITestResult result) {
if (retryCount < maxRetryCount) {
retryCount++;
return true;
}
return false;
}
}
Friday, March 8, 2019
Select an Item from the drop-down in selenium webdriver
package learning;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class Dropdown {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("https://html.com/attributes/select-size/?utm_source=network%20recirc&utm_medium=Bibblio");
//Select dropdown by Select class
Select select = new Select(driver.findElement(By.xpath("//select")));
//select required item from the above dropdown
select.selectByValue("Lesser");
}
}
Labels:
automation,
automation testing,
coding,
development,
Java,
learning,
selenium,
testing,
web driver
Dealing with modal dialog in selenium Webdriver
package test.test;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class ModelDialog {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Mutturaj\\Downloads\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("https://html.com/input-type-file/");
WebElement element = driver.findElement(By.xpath("//*[@id='fileupload']"));
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("arguments[0].scrollIntoView()", element);
element.sendKeys("C:/Users/Mutturaj/Downloads/chromedriver_win32/chromedriver.exe");
System.out.println("File Uploaded");
// activeElement() method clicks the modal dialog
driver.switchTo().activeElement();
// Click on the button, here in out case we click on the 'Don't allow'
// button
driver.findElement(By.linkText("Don't Allow")).click();
driver.findElement(By.xpath("//input[@type='submit' and @value='submit']")).click();
System.out.println("Submitted");
}
}
Labels:
automation,
automation testing,
selenium,
testing,
web driver
Handling Alert in selenium
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class AlertPopup {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Mutturaj\\Downloads\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("https://mail.rediff.com/cgi-bin/login.cgi");
driver.findElement(By.name("proceed")).click();
Alert alert = driver.switchTo().alert();
System.out.println(alert.getText());
alert.accept();
// other importent methods
/*
* alert.getText() alert.dismiss(); alert.sendKeys("text");
*/
}
}
Implicit wait in Selenium
package learning;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class MoveMouse {
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("https://www.spicejet.com");
Actions actions = new Actions(driver);
actions.moveToElement(driver.findElement(By.linkText("ADD-ONS"))).build().perform();
Thread.sleep(3000);
waitForVisa(driver,driver.findElement(By.linkText("Dubai Visa Services")),20);
driver.close();
}
//Implicit wait method
public static void waitForVisa(WebDriver driver, WebElement element,int timeout){
new WebDriverWait(driver,timeout).until(ExpectedConditions.elementToBeClickable(element));
element.click();
}
}
Labels:
automation,
implicit-wait,
selenium,
testing
Thursday, March 7, 2019
Take screenshots in selenium
package utility;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class TakeScreenshot1 {
//Takes screenshot method
public static void takepics(WebDriver driver,String filePath) throws IOException{
File file=((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
File destFile=new File(filePath);
FileUtils.copyFile(file, destFile);
}
public static void main(String[] args) throws IOException {
System.setProperty("webdriver.chrome.driver",
"C:/Users/Mutturaj.h/Desktop/Soft/chromedriver_win32/chromedriver.exe");
//Time Stamp
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
//File path where screenshot is saved
String filePath="C:/Users/Mutturaj.h/workspace/Test/Screenshots/muttu"+""+ timeStamp+".png";
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("https://www.amazon.in/");
// takepics() method is called after page loaded
TakeScreenshot1.takepics(driver,filePath);
}
}
Labels:
screeshot,
selenium,
time stamp
Monday, February 18, 2019
Retrieve data from excel , data provider and TestNG
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.testng.annotations.DataProvider;
public class PoiEx {
static Object[][] names = null;
static String filePath = "C:\\Users\\Mutturaj\\Desktop";
public static Object[][] readExcel(String filePath, String filename, String sheetName) throws IOException {
File file = new File(filePath + "\\" + filename);
FileInputStream fileInputStream = new FileInputStream(file);
Workbook workbook = null;
String fileextensionname = filename.substring(filename.indexOf("."));
if (fileextensionname.equals(".xlsx")) {
workbook = new XSSFWorkbook(fileInputStream);
} else if (fileextensionname.equals(".xls")) {
workbook = new HSSFWorkbook(fileInputStream);
}
// Get sheet by name
Sheet sheet = workbook.getSheet(sheetName);
// Count rows in the sheet
int rowCount = sheet.getLastRowNum();
int colCount = 2;
names = new Object[rowCount + 1][colCount];
for (int i = 0; i < rowCount + 1; i++) {
Row row = sheet.getRow(i);
int lastCellNumber = row.getLastCellNum();
for (int j = 0; j < lastCellNumber; j++) {
names[i][j] = (row.getCell(j).getStringCellValue());
}
}
return names;
}
@DataProvider(name = "array")
public Object[][] nana() throws IOException {
Object[][] array = PoiEx.readExcel(filePath, "muttu.xlsx", "muttu");
return array;
}
}
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class NGTest1 {
@BeforeTest()
public void loadFile() {
System.out.println("File open....");
}
@BeforeMethod
public void openBrowser() {
System.out.println("Browser opened************");
}
@AfterMethod
public void closeBrowser() {
System.out.println("**************Close browser");
}
@Test(priority = 1, dataProvider = "array", dataProviderClass = PoiEx.class)
public void add(String first, String second) {
System.out.println(first);
System.out.println(second);
}
@Test(priority = 2)
public void substract() {
System.out.println("Substraction");
}
@Test(priority = 3)
public void Multiply() {
System.out.println("Multiply");
}
@Test(priority = 4)
public void Divide() {
System.out.println("Divide");
}
@AfterTest
public void closeFile() {
System.out.println("File closed.....");
}
}
Labels:
dtaprovider,
Java,
selenium,
testng
Subscribe to:
Posts (Atom)