Hi I am trying to create report through Extent Report; the code gives no error and runs successfully but there is no html report generated. Can anyone please help below is my code -
package ca.automation.com;
import org.testng.annotations.Test;
import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import org.testng.annotations.BeforeTest;
import org.junit.Assert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.AfterClass;
public class ExtentReport {
WebDriver driver;
ExtentReports extent;
ExtentTest test;
#BeforeTest
public void startReport(){
extent = new ExtentReports("C:\\Report.html", true);
}
#Test
public void installapp() {
test = extent.startTest("installapp");
System.setProperty("webdriver.ie.driver", "C:\\Anuj\\Downloads\\IEDriverServer_Win32_2.46.0\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
driver.manage().window().maximize();
driver.get("https://www.tripadvisor.com/");
String Title = driver.getTitle();
Assert.assertTrue(Title.contains("Trip"));
extent.endTest(test);
}
}
Append extent.flush(); at the end of the test method to write all the test logs to the report file.
Check the documentation: http://extentreports.relevantcodes.com/java/#start-end-tests`
I think extent.flush(); is missing at the end.
the flush() and the close() needs to be called for the extent object to write the changes to the file. Call to close() must be done just before end of the Test as it closes the output stream
Call flush() in #AfterMethod method and close() in #AfterSuite method
#AfterMethod
public void afterMethod() {
extent.flush();
}
#AfterSuite
public void afterSuiteMethod() {
extent.close();
}
References :- ExtentReports Examples for Java
Here you go, just copy paste the working code in your project.
import org.testng.annotations.Test;
import com.dell.patientregister.mobilewallet.test.PatientRegister_SmokeTest;
import com.relevantcodes.extentreports.ExtentReports;
import org.testng.annotations.BeforeTest;
import org.junit.Assert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class ExtentReport {
WebDriver driver;
ExtentReports extent;
#Test
public void installapp() {
ExtentReports extent = ExtentReports.get(ExtentReport.class);
extent.init("myreport.html", true);
extent.startTest(" Install App");
System.setProperty("webdriver.ie.driver", "C:\\Anuj\\Downloads\\IEDriverServer_Win32_2.46.0\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
driver.manage().window().maximize();
driver.get("https://www.tripadvisor.com/");
String Title = driver.getTitle();
Assert.assertTrue(Title.contains("Trip"));
extent.endTest();
}
}
Please try this:
import org.testng.annotations.Test;
import com.dell.patientregister.mobilewallet.test.PatientRegister_SmokeTest;
import com.relevantcodes.extentreports.ExtentReports;
import org.testng.annotations.BeforeTest;
import org.junit.Assert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class ExtentReport {
WebDriver driver;
ExtentReports extent;
#Test
public void installapp() {
ExtentReports extent = ExtentReports.get(ExtentReport.class);
extent.init("myreport.html", true);
test = extent.startTest(" Install App");
System.setProperty("webdriver.ie.driver","C:\\Anuj\\Downloads\\IEDriverServer_Win32_2.46.0\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
driver.manage().window().maximize();
driver.get("https://www.tripadvisor.com/");
String Title = driver.getTitle();
Assert.assertTrue(Title.contains("Trip"));
test.Log(LogStatus.Pass, "Login Successful");
extent.endTest(test);
extent.flush();
extent.close();
}
}
Add extent.flush() at the end.
Please check if all the configurations are executed.i.e. your #BeforeClass,#AfterClass etc...
Sometimes they are skipped and so the extent.html is not generated.
So add the following tag "alwaysRun=true" with the testng annotation
e.g.#AfterMethod(alwaysRun = true)
It works :)
I got this error in testng project even though extent.flush() was added in code. The reason for not generating error was during execution exent.flush() line was not executed due to one of failed assertion. After removing that assertion script was working fine and file got generated as usual.
I had the same problem in generating Extent reports. The following solution worked for me.
Please add these commands at the end:
extent.EndTest(test);
extent.Flush();
Make sure you add this version from NuGet Packages - ExtentReports 2.41.0
If it is not working even after adding extent.flush(), Try by adding below dependencies in pom.xml
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.8.3</version>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.8.11.1</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver</artifactId>
<version>3.0.4</version>
</dependency>
In your test, you are not logging anything in report. If nothing is logged, no report would generate. As given below, try logging something in report. Also as per status of assertion, you can log PASS/FAIL in extentreport
test.log(LogStatus.INFO, "Test Started");
test.log(LogStatus.ERROR, "Test FAILED");
Adding bson-xxx.jar solved my same issue. Can try it out.
Related
Cloned the project from github at location :
C:\Automation\CC_Regression_Automation\CC_Regression
When trying to run testng.xml using eclipse all goes well.
Getting Cannot instantiate class testCases.LoginPage when trying to run the code using command line.
**Loginpage.java**
package testCases;
import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import com.relevantcodes.extentreports.ExtentTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import utils.DriverUtil;
import utils.Loggers;
import utils.ReportGenerator;
import utils.WebPageUtils;
public class LoginPage extends Base{
/*Members of the current Test Class. The number varies from script
to script depending on the variables and verifications required*/
public WebDriver driver;
private String currentSitePath;
private String testCaseName=getClass().getName().substring(10);
ExtentTest parentTest =ReortGenerator.initializeParentTest(getClass().getName().substring(10),"Testing Login Page");
//Function to Navigate to a particular URL inside CC
public void navigateToURL(WebDriver driver){
siteURL="";
this.driver.navigate().to(baseurl+siteURL);
}
#Test // Main Test Flow for the Script
public void executeScript() throws IOException{
System.out.println("*******************");
System.out.println("launching chrome browser");
//Test Case Author assignment in Reports
ReportGenerator.assignAuthor(parentTest,"Garima");
//Setting up Browser Instance
this.driver=driverIns();
//Navigating to the required page in CC
navigateToURL(this.driver);
Sleep(5000);
String strPageTitle = this.driver.getTitle();
System.out.println(strPageTitle);
//Start Logs
Loggers.startCurrentTestCaseExecution(this.driver);
try{
ReportGenerator.verifyNavigation(this.driver, "Control Center", parentTest,testCaseName,"Yes");
Code snippent of Base.java
//Function to instantiate the WebDriver instance based on the Browser
selected for Windows
public WebDriver driverInsWindows(){
isExtensionEnabled();
setbaseURL();
try {
switch(getBrowser()+isExtensionEnabled.toString()){
case "Chromefalse":
System.setProperty("webdriver.chrome.driver", "./Win/Drivers/chromedriver.exe");
driver=new ChromeDriver();
driver.get(baseurl);
break;
case "Chrometrue":
System.setProperty("webdriver.chrome.driver", "./Win/Drivers/chromedriver.exe");
driver=invokeChromeBrowserwithExtension();
driver.get("https://www.google.com");
break;
case "Internet Explorer":
System.setProperty("webdriver.internetexplorer.driver",
"./Win/Drivers/internetexplorerdriver.exe");
driver=new InternetExplorerDriver();
driver.get(baseurl);
break;
case "Firefox":
driver=new FirefoxDriver();
driver.get(baseurl);
break;
default:
//new PascalBaseClass();
}
} catch (IOException e) {
}
Executing below command via cmd:-
java -cp C:\Automation\CC_Regression_Automation\CC_Regression\bin;C:\Automation\CC_Regression_Automation\CC_Regression\lib\* org.testng.TestNG testng.xml
Blockquote
Your problem might be this line:
ExtentTest parentTest =ReortGenerator.initializeParentTest(getClass().getName().substring(10),"Testing Login Page")
Since I'm seeing other references to ReportGenerator
I doubt you typed all that code, and it's a copy/paste so I'm not sure why your IDE doesn't flag it, or is there somehow another object called ReortGenerator
I am using Selenium to automate the CEF application. I am successfully able to perform operations like click etc. But not able to take the screenshot using Selenium driver. As it is very much required feature for automation. How can I do this?
I'm using the following:
CEF application - sample application provided by CEF
selenium jar - selenium-server-standalone-3.0.1
cef_binary_3.2924.1564.g0ba0378_windows64_client
chromedriver
Find the below code:
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.chrome.ChromeOptions;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.OutputType;
public class Example {
public static void main(String[] args) {
// Path to the ChromeDriver executable.
System.setProperty("webdriver.chrome.driver", "D:/CEFTesting/chromedriver.exe");
// Path to the CEF executable.
ChromeOptions options = new ChromeOptions();
options.setBinary("D:/CEFTesting/cef_binary_3.2924.1564.g0ba0378_windows64_client/Release/cefclient.exe");
WebDriver driver = new ChromeDriver(options);
driver.get("http://www.google.com/xhtml");
sleep(3000); // Let the user actually see something!
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys("ChromeDriver");
searchBox.submit();
sleep(5000); // Let the user actually see something!
String screenshotBase64 = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BASE64);
System.out.println(screenshotBase64);
sleep(5000); // Let the user actually see something!
driver.quit();
}
}
I am facing the error.
I'm using the following:
CEF application - Sample application provided by CEF (link - https://bitbucket.org/chromiumembedded/cef/wiki/UsingChromeDriver.md)
selenium jar - selenium-server-standalone-3.0.1
cef_binary_3.2924.1564.g0ba0378_windows64_client
chromedriver
Here is the code:
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.chrome.ChromeOptions;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.OutputType;
public class Example {
public static void main(String[] args) {
// Path to the ChromeDriver executable.
System.setProperty("webdriver.chrome.driver", "D:/CEFTesting/chromedriver.exe");
// Path to the CEF executable.
ChromeOptions options = new ChromeOptions();
options.setBinary("D:/CEFTesting/cef_binary_3.2924.1564.g0ba0378_windows64_client/Release/cefclient.exe");
WebDriver driver = new ChromeDriver(options);
driver.get("http://www.google.com/xhtml");
sleep(3000); // Let the user actually see something!
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys("ChromeDriver");
searchBox.submit();
sleep(5000); // Let the user actually see something!
String screenshotBase64 = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BASE64);
System.out.println(screenshotBase64);
sleep(5000); // Let the user actually see something!
driver.quit();
}
}
below is the code : im not able launch the browser using the code . please suggest the solution
import org.openqa.selenium.Test;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.Assert;
import org.testng.annotations.*;
public class NewTest {
public WebDriver driver;`enter code here`
#Test
public void VerifyHomepageTitle() {
WebDriver driver = new FirefoxDriver();
driver.navigate().to("http://google.com");
driver.quit();
}
}
You are closing the web page immediately after opening using 'driver.quit()'. Comment this code and try you will see the difference.
I am trying to download payslip as PDF from greytip web portal using chrome driver.i am trying to click on link salary by using "driver.findElement(By.linkText("Salary")).click();".But i am unable to click the link and failed with following exception.
Error
org.openqa.selenium.WebDriverException: Element is not clickable at point (198, 139). Other element would receive the click: ... (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 37 milliseconds
And also when ever i ran the script chrome open one extra tab for bit torrent.so here when i ran the program one tab is opened for "https://psdpl.greytip.in" and another tab is opened for bittorrent.How can i handle not to open another bittorrent tab when i ran the program.
Here i am attaching the code and screen shots.enter image description here
Code
package com.webdriver.tests;
import static org.junit.Assert.*;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class PaySlipPDF {
private WebDriver driver;
private String baseUrl;
private StringBuffer verificationErrors = new StringBuffer();
#Before
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\soleti\\D-Drive\\Selenium\\chromedriver\\chromedriver.exe");
driver = new ChromeDriver();
baseUrl = "https://psdpl.greytip.in/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#Test
public void testPayslip() throws Exception {
driver.get(baseUrl + "/login.do");
driver.findElement(By.id("j_username")).clear();
driver.findElement(By.id("j_username")).sendKeys("101786");
driver.findElement(By.id("j_password")).clear();
driver.findElement(By.id("j_password")).sendKeys("password");
driver.findElement(By.id("login-button")).click();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
//driver.findElement(By.xpath("//*[#id='home-page']/div[1]/div[1]/ul/li[2]/a")).click();
WebElement elementToClick = driver.findElement(By.xpath("//*[#id='home-page']/div[1]/div[1]/ul/li[2]/a"));
System.out.println(elementToClick);
// Scroll the browser to the element's Y position
((JavascriptExecutor) driver).executeScript("window.scrollTo(0,"+elementToClick.getLocation().y+")");
// Click the element
elementToClick.click();
//driver.findElement(By.linkText("Salary")).click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.linkText("View Payslips")).click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
new Select(driver.findElement(By.id("payroll"))).selectByVisibleText("Mar 2012");
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.findElement(By.className("btn btn-gts-print")).click();
}
#After
public void tearDown() throws Exception {
//driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
}
It is the BitTorrent toolbar that is causing the issue. It is launched into Chrome by an extension. Either uninstall it completely, or force Selenium to tell Chrome to disable all extensions. This can be done using the ChromeOptions class:
http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/chrome/ChromeOptions.html
Use the addArgument method and give it this arguement, which tells Chrome to disable all user extensions:
--disable-extensions
Check the browser settings and see if the default homepage has been changed by the bit torrent toolbar that is installed. If not needed, try to uninstall the bitTorrent toolbar from the browser and rerun your selenium program. Hope this helps.
This problem is specificaly related to chrome installed on your system..Uninstall the google chrome and re-install it..That will fix your problem..:)
I use Selenium Web Driver with JUnit in Eclipse. I want diminish my code by creating new classes for repitable steps. I want store this classes in separate files for convenience. For example this one of such classes:
import org.openqa.selenium.ie.InternetExplorerDriver;
//Login
public class Login {
private InternetExplorerDriver driver;
String url;
String name;
String password;
String language;
public Login (String ur, String nam, String pass, String lang){
url=ur;
name=nam;
password=pass;
language=lang;
}
public void log_in (){
driver.get(url);
driver.findElement(By.name("username")).sendKeys(name);
driver.findElement(By.name("password")).sendKeys(password);
driver.findElement(By.name("language")).sendKeys(language);
driver.findElement(By.name("logon_action")).click();
}
}
This is my main test:
package bobsworld;
import init.Login;
import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class WebTest {
private WebDriver driver;
#Test
public void testUnit() throws Exception {
//Open new window
System.setProperty("webdriver.ie.driver", "IEDriverServer.exe");
driver = new InternetExplorerDriver();
//Login
init.Login login = new init.Login ("myurl", "log","pas","English");
login.log_in();
}
The problem is with object driver. I get java.lang.NullPointerException and can't execute test. How should I organize call to Login and code to make my test work?
What you try to achieve is a quite popular approach called "PageObjects".
Your issues:
Use some checkstyle tool to improve code quality
Do a whole lot of research about Java in general
The Login Class or "init.Login" (it's quite unusual to have a part of the package as prefix for the class) is not able to use the same WebDriver instance because you don't forward it. Normally you would have opened a second WebDriver instance but that seems to be impossible though I don't know your setup. You have to forward the "driver" as a parameter to the constructor of the Login class.
Try to use real PageObjects as described here inluding PageFactory to improve readability and maintainability