Hybrid Framework Not able to click on the Login on zoho.com | Selenium Forum
A
Adil Alami Posted on 03/07/2020

After the modification on the video 26 where you made the new getElement() function and created the new ReportUtil class along with its reporter object to access the TestBase class, I'm still able to launch the browser but no longer able to do the click and the Type functions, maybe you can help based on the following screenshots and also I don't recall you have made any modification the CustomListener class.

 

*****************************************************************

*****************************************************************

package driver;

import java.time.Duration;
import java.util.Properties;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;

import util.ReportUtil;

// KeywordUtility <== GenericKeywords <== ApplicationKeywords
// ApplicationKeywords is extending GenericKeywords
// GenericKeywords is extending KeywordUtility

public class KeywordUtility {
public Properties prop;
public ReportUtil reporter;
public WebDriver driver;

//*********************************Utility Function*****************//
public void log(String message) {
System.out.println(message);
reporter.log(message);
}

public void logFailure(String msg) { // only fails in extent report
System.out.println(msg);
reporter.logFailure(msg);
}

public void failAndStopTest(String msg) {//fail the TestNG and the Extent Report but will stop the test
System.out.println(msg);
reporter.failAndStopTest(msg);// will stop and report the test case execution
}

public void failAndDoNotStopTest(String msg) {//fail the TestNG and the Extent Report but will continue the execution
System.out.println(msg); // will fail in Extent report
reporter.failAndDoNotStopTest(msg); // will fail in TestNG
}


//****************************************************************************
// single location - entire framework - extract the element will make sure - present and visible
public WebElement getElement(String locatorkey) {
//present
if(!isElementPresent(locatorkey)) {
// report failure and stop the test
failAndDoNotStopTest("Element not present on page"+locatorkey);
}
//visible
if(!isElementVisible(locatorkey)) {
// report failure and stop the test
failAndStopTest("Element not visible on page"+locatorkey);
}

WebElement e = null;
try {
e = driver.findElement(getLocator(locatorkey));
}catch(Exception ex) {
//If it reachs this block, that means there's a failure
//report a failure and stop the test
failAndStopTest("Element not visible on page"+locatorkey);
}
return e;

}
//****************************************************************************
//This function will return true if the element is present
//false if the element is not present
public boolean isElementPresent(String locatorkey) {
try {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.presenceOfElementLocated(getLocator(locatorkey)));
}catch(Exception e) {
//If you reach over this section, that means, there's some exception and the element is not present
return false;
}
//If you reach this block, that means the element is present
return true;
}

//****************************************************************************
//This function will return true if the element is visible
//false if the element is not visible
public boolean isElementVisible(String locatorkey) {
try {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.invisibilityOfElementLocated(getLocator(locatorkey)));
}catch(Exception e) {
//If you reach over this section, that means, there's some exception and the element is not visible
return false;
}
//If you reach this block, that means the element is visible
return true;
}

//****************************************************************************
public By getLocator(String locatorkey) {
By by = null;

if(locatorkey.endsWith("_id"))
by = By.id(prop.getProperty(locatorkey));
else if(locatorkey.endsWith("_name"))
by = By.name(prop.getProperty(locatorkey));
else if(locatorkey.endsWith("_css"))
by = By.cssSelector(prop.getProperty(locatorkey));
else
by = By.xpath(prop.getProperty(locatorkey));

return by;
}

public void wait(int time) {
try {
Thread.sleep(time*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

public void takeScreenshot() {

}

}

*****************************************************************

*****************************************************************

package driver;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeDriverService;
import org.openqa.selenium.edge.EdgeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;

import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;

public class Generickeywords extends KeywordUtility{

//*************************Keyword Functions**********************//

public void openBrowser(String browserName) {
log("Opening browser: "+browserName);
if(browserName.equalsIgnoreCase("Mozilla")) {
System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE, "logs\\firefox.log");
FirefoxOptions options = new FirefoxOptions();
options.setPageLoadStrategy(PageLoadStrategy.EAGER);
FirefoxProfile prof = new FirefoxProfile();// new profile
prof.setPreference("dom.webnotifications.enabled", false);
options.setProfile(prof);
driver = new FirefoxDriver(options);
}else if(browserName.equalsIgnoreCase("Chrome")){
System.setProperty(ChromeDriverService.CHROME_DRIVER_LOG_PROPERTY, "logs\\chrome.log");
System.setProperty(ChromeDriverService.CHROME_DRIVER_SILENT_OUTPUT_PROPERTY, "true");
ChromeOptions ops = new ChromeOptions();
//ops.setBinary("");
//ops.setPageLoadStrategy(PageLoadStrategy.NORMAL);
ops.addArguments("--disable-notifications");
ops.addArguments("--start-maximized");
driver = new ChromeDriver(ops);
}
else if(browserName.equalsIgnoreCase("Edge")) {
System.setProperty(EdgeDriverService.EDGE_DRIVER_SILENT_OUTPUT_PROPERTY,"true");
EdgeOptions options = new EdgeOptions();
options.setPageLoadStrategy(PageLoadStrategy.EAGER);
//options.setBinary(new File(""));
options.addArguments("--disable-notifications");
options.addArguments("--start-maximized");
driver = new EdgeDriver(options);
}
// dynamic wait- not pause
// global time out- all driver.findelement
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}

public void navigate(String urlkey) {
log("Navigating to: "+prop.getProperty(urlkey));
driver.get(prop.getProperty(urlkey));
}


public void click(String locatorkey) {
log("Clicking on the :"+locatorkey);
//driver.findElement(getLocator(locatorkey)).click();
getElement(locatorkey).click();
}


public void type(String locatorkey, String data) {

log("Typing in: "+locatorkey+", Data is: "+data);
//driver.findElement(getLocator(locatorkey)).sendKeys(data);
getElement(locatorkey).sendKeys(data);
}


public void waitForPageToLoad() {
JavascriptExecutor js = (JavascriptExecutor)driver;
int i=0;

while(i!=10) {
String state = (String)js.executeScript("return document.readyState;");
System.out.println(state);

if(state.contentEquals("complete"))
break;
else
wait(2);
i++;

}
//check for jquery status
i=0;
while(i!=10) {

Long d = (Long) js.executeScript("return jQuery.active;");
System.out.println(d);
if(d.longValue() == 0 )
break;
else
wait(2);
i++;
}
}

}

*****************************************************************

*****************************************************************

package testbase;
//DataProvider implementation
//Listeners Implementation
//Reporting multiple failure in one test using soft assertion
//Maven execution
//Parallel Execution
//Mail the reports
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import javax.imageio.ImageIO;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.Point;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeDriverService;
import org.openqa.selenium.edge.EdgeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;

import com.aventstack.extentreports.Status;

import util.ReportUtil;

public class TestBase {
public ReportUtil reporter = null;
public String browser;
public WebDriver driver;
public Properties prop=null;
public String currentTestName;

@BeforeMethod(alwaysRun=true)
public void init(ITestContext con, ITestResult result) {
System.out.println("--------@BeforeMethod-------------");
System.out.println("The testcase name is: "+result.getMethod().getMethodName().toUpperCase());

currentTestName = result.getMethod().getMethodName().toUpperCase();
reporter = new ReportUtil();
reporter.init(currentTestName);

result.setAttribute("reporter", reporter.getTest());


// init the variable
browser = con.getCurrentXmlTest().getParameter("browser");

// find the browser group name from test method parameters
// String groupNames[] = con.getAllTestMethods()[0].getGroups();
// String browserGroup = "";
// for(String g : groupNames) {
// if(g.startsWith("browsergroup")) {
// browserGroup = g;
// break;
// }
// }
// System.out.println("Browser group is: "+browserGroup);
// browser = con.getCurrentXmlTest().getParameter(browserGroup);

System.out.println("Browser is: "+browser);

//the trick is the name of the group"brousergroup1", should be the same as one on the testNG parameter line 13
}

@AfterMethod(alwaysRun=true)
public void quit() {
// rep.flush();
reporter.flush();
}

public void log(String msg) {
System.out.println(msg);
reporter.log(msg);
}
public void logFailure(String msg) { // only fails in extent report
System.out.println(msg);
reporter.logFailure(msg);
}



public WebDriver launchBrowser(String browserName) {

if(browserName.equals("Mozilla")) {
System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE, "logs\\firefox.log");
FirefoxOptions options = new FirefoxOptions();
options.setPageLoadStrategy(PageLoadStrategy.EAGER);
FirefoxProfile prof = new FirefoxProfile();// new profile
prof.setPreference("dom.webnotifications.enabled", false);
options.setProfile(prof);
driver = new FirefoxDriver(options);
}else if(browserName.equals("Chrome")) {
//System.setProperty(ChromeDriverService.CHROME_DRIVER_LOG_PROPERTY, "logs\\chrome.log");
System.setProperty(ChromeDriverService.CHROME_DRIVER_SILENT_OUTPUT_PROPERTY, "true");
ChromeOptions ops = new ChromeOptions();
//ops.setBinary("");
//ops.setPageLoadStrategy(PageLoadStrategy.NORMAL);
ops.addArguments("--disable-notifications");
ops.addArguments("--start-maximized");
driver = new ChromeDriver(ops);
}
else if(browserName.equals("Edge")) {
System.setProperty(EdgeDriverService.EDGE_DRIVER_SILENT_OUTPUT_PROPERTY,"true");
EdgeOptions options = new EdgeOptions();
options.setPageLoadStrategy(PageLoadStrategy.EAGER);
//options.setBinary(new File(""));
options.addArguments("--disable-notifications");
options.addArguments("--start-maximized");
driver = new EdgeDriver(options);
}


// dynamic wait- not pause
// global time out- all driver.findelement
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);


// prop file init
try {

prop = new Properties();
FileInputStream fs = new FileInputStream(System.getProperty("user.dir")+"//src/test//resources//project.properties");
prop.load(fs);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return driver;
}

public void takeScreenShot(String filePath){
// take screenshot
// save screenshot in reports screenshot folder
// add the screenshots in the reports
File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
// get the dynamic folder name
FileUtils.copyFile(srcFile, new File(filePath));
//test.addScreenCaptureFromPath("path of image", "xxxx");

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public void getElementScreenshot(WebElement ele, String filePath)
{
// Get entire page screenshot
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage fullImg;
try {
fullImg = ImageIO.read(screenshot);
// Get the location of element on the page
Point point = ele.getLocation();

// Get width and height of the element
int eleWidth = ele.getSize().getWidth();
int eleHeight = ele.getSize().getHeight();

// Crop the entire page screenshot to get only element screenshot
BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(),
eleWidth, eleHeight);
ImageIO.write(eleScreenshot, "png", screenshot);

// Copy the element screenshot to disk
File screenshotLocation = new File(filePath);
FileUtils.copyFile(screenshot, screenshotLocation);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

*****************************************************************

*****************************************************************

package util;

import org.testng.asserts.SoftAssert;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import reports.ExtentManager;

public class ReportUtil {
public ExtentReports rep;
public ExtentTest test;
public SoftAssert softAssert;



public void log(String msg) {
// System.out.println(msg);
test.log(Status.INFO, msg);
}


public void init(String currentTestName) {
rep = ExtentManager.getReports(); //Object to access getReport function in the ExtentManager class
test = rep.createTest(currentTestName);// Creating Test Case for the ExtentReport based on the dynamically retrieved TestName:EX TestA
softAssert = new SoftAssert();
// return test; // Returns back the test object
}

public void logFailure(String msg) { // only fails in extent report
// System.out.println(msg);
test.log(Status.FAIL, msg);
}

public void failAndStopTest(String msg) {//fail the TestNG and the Extent Report but will stop the test
failAndDoNotStopTest(msg);
softAssert.assertAll();// will stop and report the test case execution
}

public void failAndDoNotStopTest(String msg) {//fail the TestNG and the Extent Report but will continue the execution
logFailure(msg); // will fail in Extent report
softAssert.fail(msg); // will fail in TestNG
}

public ExtentTest getTest() {
return test; // will return the ExtentTest Object

}


public void flush() {
rep.flush();
}

}

*****************************************************************

*****************************************************************

Properties file:

url=https://www.zoho.com/
login_link_xpath=//a[@class='zh-login']
username_text_id=login_id
login_button_css=button#nextbtn
password_text_id=password

 

 

 

 

 


A
Ashish Thakur Replied on 03/07/2020

Please zip and send your project


A
Adil Alami Replied on 03/07/2020

Done


A
Adil Alami Replied on 04/07/2020

I was hoping for an answer before the weekend is over so that I can progress before the Monday class. But it looks like it's not going to happen. 


A
Ashish Thakur Replied on 06/07/2020

As i debugged your code. i found the error at KeywordUtility.java file

wait.until(ExpectedConditions.invisibilityOfElementLocated(getLocator(locatorkey)));

should be

wait.until(ExpectedConditions.visibilityOfElementLocated(getLocator(locatorkey)));


A
Adil Alami Replied on 06/07/2020

Good Catch

Thank you

you're the BEST