Salesforce - W8D2-2
~30 mins
TestNG
package com.framework.testng.api.base;
import java.io.IOException;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import com.framework.selenium.api.base.SeleniumBase;
import com.framework.utils.DataLibrary;
public class ProjectSpecificMethods extends SeleniumBase {
@DataProvider(name = "fetchData", indices = 0)
public Object[][] fetchData() throws IOException {
return DataLibrary.readExcelData(excelFileName);
}
@BeforeMethod
public void preCondition() {
startApp("chrome", false, "https://login.salesforce.com/");
setNode();
}
public void postCondition() {
close();
}
}
package com.framework.testng.api.base;
import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;
public class RetryEngine implements IRetryAnalyzer {
private int count = 0;
private static int maxTry = 0;
@Override
public boolean retry(ITestResult iTestResult) {
if (!iTestResult.isSuccess()) {
if (count < maxTry) {
count++;
iTestResult.setStatus(ITestResult.FAILURE);
return true;
} else {
iTestResult.setStatus(ITestResult.FAILURE);
}
} else {
iTestResult.setStatus(ITestResult.SUCCESS);
}
return false;
}
}
package com.framework.utils;
import java.io.IOException;
import javax.sound.midi.SysexMessage;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class DataLibrary {
public static Object[][] readExcelData(String excelfileName) {
XSSFWorkbook wbook;
Object[][] data = null ;
try {
wbook = new XSSFWorkbook("./data/"+excelfileName+".xlsx");
XSSFSheet sheet = wbook.getSheetAt(0);
int rowCount = sheet.getLastRowNum();
int colCount = sheet.getRow(0).getLastCellNum();
data = new Object[rowCount][colCount];
for (int i = 1; i <= rowCount; i++) {
for (int j = 0; j < colCount; j++) {
data[i-1][j] = sheet.getRow(i).getCell(j).getStringCellValue();
}
}
wbook.close();
} catch (IOException e) {
System.err.println(e.getMessage());
}
return data;
}
}
package com.framework.utils;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.openqa.selenium.remote.UnreachableBrowserException;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeSuite;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.MediaEntityBuilder;
import com.aventstack.extentreports.MediaEntityModelProvider;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.reporter.configuration.ChartLocation;
import com.aventstack.extentreports.reporter.configuration.Theme;
import com.framework.selenium.api.base.DriverInstance;
public abstract class Reporter extends DriverInstance {
private static ExtentReports extent;
private static final ThreadLocal<ExtentTest> parentTest = new ThreadLocal<ExtentTest>();
private static final ThreadLocal<ExtentTest> test = new ThreadLocal<ExtentTest>();
private static final ThreadLocal<String> testName = new ThreadLocal<String>();
private String fileName = "result.html";
private String pattern = "dd-MMM-yyyy HH-mm-ss";
public String testcaseName, testDescription, authors, category, dataFileName, dataFileType, excelFileName;
public static String folderName = "";
@BeforeSuite(alwaysRun = true)
public synchronized void startReport() {
String date = new SimpleDateFormat(pattern).format(new Date());
folderName = "reports/" + date;
File folder = new File("./" + folderName);
if (!folder.exists()) {
folder.mkdir();
}
ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter("./" + folderName + "/" + fileName);
htmlReporter.config().setTestViewChartLocation(ChartLocation.BOTTOM);
htmlReporter.config().setChartVisibilityOnOpen(!true);
htmlReporter.config().setTheme(Theme.STANDARD);
htmlReporter.config().setDocumentTitle("Salesforce");
htmlReporter.config().setEncoding("utf-8");
htmlReporter.config().setReportName("Salesforce");
htmlReporter.setAppendExisting(true);
extent = new ExtentReports();
extent.attachReporter(htmlReporter);
}
@BeforeClass(alwaysRun = true)
public synchronized void startTestCase() {
ExtentTest parent = extent.createTest(testcaseName, testDescription);
parent.assignCategory(category);
parent.assignAuthor(authors);
parentTest.set(parent);
testName.set(testcaseName);
}
public synchronized void setNode() {
ExtentTest child = parentTest.get().createNode(getTestName());
test.set(child);
}
public abstract long takeSnap();
public void reportStep(String desc, String status, boolean bSnap) {
synchronized (test) {
// Start reporting the step and snapshot
MediaEntityModelProvider img = null;
if (bSnap && !(status.equalsIgnoreCase("INFO") || status.equalsIgnoreCase("skipped")
)) {
long snapNumber = 100000L;
snapNumber = takeSnap();
try {
img = MediaEntityBuilder
.createScreenCaptureFromPath("./../../" + folderName + "/images/" + snapNumber + ".jpg")
.build();
} catch (IOException e) {
}
}
if (status.equalsIgnoreCase("pass")) {
test.get().pass(desc, img);
} else if (status.equalsIgnoreCase("fail")) { // additional steps to manage alert pop-up
test.get().fail(desc, img);
throw new RuntimeException("See the reporter for details.");
} else if (status.equalsIgnoreCase("warning")) {
test.get().warning(desc, img);
} else if (status.equalsIgnoreCase("skipped")) {
test.get().skip("The test is skipped due to dependency failure");
} else if (status.equalsIgnoreCase("INFO")) {
test.get().info(desc);
}
}
}
public void reportStep(String desc, String status) {
reportStep(desc, status, true);
}
@AfterSuite(alwaysRun = true)
public synchronized void endResult() {
try {
if (getDriver() != null) {
getDriver().quit();
}
} catch (UnreachableBrowserException e) {
}
extent.flush();
}
public String getTestName() {
return testName.get();
}
public Status getTestStatus() {
return parentTest.get().getModel().getStatus();
}
}
Salesforce
package com.salesforce.pages;
import com.framework.selenium.api.design.Locators;
import com.framework.testng.api.base.ProjectSpecificMethods;
public class HomePage extends ProjectSpecificMethods{
public HomePage verifyHomePage() {
verifyDisplayed(locateElement(Locators.XPATH, "//span[text()='Home']"));
reportStep("Homepage is loaded", "pass");
return this;
}
}
package com.salesforce.pages;
import com.framework.selenium.api.design.Locators;
import com.framework.testng.api.base.ProjectSpecificMethods;
public class LoginPage extends ProjectSpecificMethods{
public LoginPage enterUsername(String data) {
clearAndType(locateElement(Locators.ID, "username"), data);
reportStep(data+" entered successfully","pass");
return this;
}
public LoginPage enterPassword(String data) {
clearAndType(locateElement(Locators.ID, "password"), data);
reportStep(data+" entered successfully","pass");
return this;
}
public HomePage clickLogin() {
click(locateElement(Locators.ID, "Login"));
reportStep("Login button clicked successfully", "pass");
return new HomePage();
}
}
TC001_VerifyLogin.java
package com.salesforce.testcases;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import com.framework.testng.api.base.ProjectSpecificMethods;
import com.salesforce.pages.LoginPage;
public class TC001_VerifyLogin extends ProjectSpecificMethods{
@BeforeTest
public void setValues() {
testcaseName = "VerifyLogin";
testDescription ="Verify Login functionality with positive data";
authors="Hari";
category ="Smoke";
excelFileName="Login";
}
@Test(dataProvider = "fetchData")
public void runLogin(String username, String password) {
new LoginPage()
.enterUsername(username)
.enterPassword(password)
.clickLogin()
.verifyHomePage();
}
}
config.properties
# Server Details
url=http://leaftaps.com/opentaps/control/main
# Authentication
username=Demosalesmanager
password=crmsfa