Not able to create object of ExtentReports - java

I am new to extent reporting. I am using Selenium Webdriver and want to use Extent reports with it.
But my code is not able to create ExtentReport object.
package com.code.draft;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;
public class TestReport {
ExtentReports reports;
ExtentTest logger;
WebDriver driver;
public void start(){
reports = new ExtentReports("C:\\User\\Test\\Report\\Report.html"); //Exception at this line reports object = null
driver = new FirefoxDriver();
driver.get("http://www.google.com");
logger = reports.startTest("Verify Title");
logger.log(LogStatus.INFO, "Starting Browser");
reports.endTest(logger);
}
public static void main(String[] args) {
TestReport report = new TestReport();
report.start();
}
}
The above code is giving exception as :
Exception in thread "main" java.lang.NoSuchFieldError: VERSION_2_3_23
at com.relevantcodes.extentreports.HTMLReporter.start(HTMLReporter.java:76)
at com.relevantcodes.extentreports.Report.attach(Report.java:314)
at com.relevantcodes.extentreports.ExtentReports.<init>(ExtentReports.java:85)
at com.relevantcodes.extentreports.ExtentReports.<init>(ExtentReports.java:419)
at com.code.draft.TestReport.start(TestReport.java:19)
at com.code.draft.TestReport.main(TestReport.java:29)
Using the below configuration :
<dependency>
<groupId>com.relevantcodes</groupId>
<artifactId>extentreports</artifactId>
<version>2.41.2</version>
</dependency>
if anyone have idea. Please help.

I tested your code. It shows no exception at my end. But to get your HTML report you need to flush using reports.flush() just before reports.endTest(logger);.

Related

How to send the login credentials using HtmlUnitDriver and Selenium Java

I'm tring to send the login credentials within the username and password field https://www.vignanits.ac.in/server/moodle/login/index.php which needed to be automated using HtmlUnitDriver but facing NoSuchElementException.
Code trials:
package leela;
import org.openqa.selenium.By;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import com.gargoylesoftware.htmlunit.WebClient;
public class LeelaHtmlUnit {
public static void main(String[] args) throws InterruptedException {
HtmlUnitDriver driver = new HtmlUnitDriver(true);
driver.get("https://www.vignanits.ac.in/server/moodle/login/index.php");
System.out.println(driver.getCurrentUrl()+driver.getTitle());
driver.findElement(By.id("username")).sendKeys("xyz");
driver.findElement(By.id("password")).sendKeys("xyz");
driver.findElement(By.id("loginbtn")).click();
System.out.println(driver.getCurrentUrl()+driver.getTitle());
}
}
enter code here
Ideally, to use htmlunit-driver you need to download htmlunit-driver-2.42.0-jar-with-dependencies.jar the latest release as of today.
The below code block works perfect at my end:
import org.openqa.selenium.By;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class HtmlUnitDriverDemo {
public static void main(String[] args) {
HtmlUnitDriver driver = new HtmlUnitDriver();
driver.get("https://www.vignanits.ac.in/server/moodle/login/index.php");
System.out.println(driver.getCurrentUrl()+driver.getTitle());
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("username")));
driver.findElement(By.id("password")).sendKeys("xyz");
driver.findElement(By.id("loginbtn")).click();
}
}
Console Output:
https://www.vignanits.ac.in/server/moodle/login/index.phpVIGNAN MOODLE: Log in to the site
Reference
You can find a detailed discussion on NoSuchElementException in:
NoSuchElementException, Selenium unable to locate element

java.lang.module.FindException: while using selenium

I am fairly new to selenium, and I was trying to use some of the scripts being used in tutorials for my practice. I downloaded all the required .JAR files (Chrome drivers, Selenium Java and Stand Alone server) and added it to the path in Eclipse.
Below is the Code which I am trying to run to access a Weblink and then trying to verify if a user is able to log in successfully or not.
package learnautomation;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class practice {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "Mypath\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.gcrit.com/build3/admin/");
driver.findElement(By.name("username")).sendKeys("admin");
driver.findElement(By.name("password")).sendKeys("admin#123");
driver.findElement(By.id("tdb1")).click();
String url = driver.getCurrentUrl();
if (url.equals("http://www.gcrit.com/build3/admin/index.php")){
System.out.println("Successful");
}
else {
System.out.println("Unsuccessful");
}
driver.close();
}
}
While doing this I am getting this error:
"Error occurred during initialization of boot layer
java.lang.module.FindException: Module seleniumnew not found"
Also, import org.openqa.selenium.chrome.ChromeDriver; it says this is not accessible as well when I just hover over it.
try this, just edit paths and package name.
package navi;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Avi {
public static WebDriver driver;
WebDriverWait wait5s = new WebDriverWait(driver,5);
#BeforeClass
public static void setUpClass() {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\pburgr\\Desktop\\chromedriver\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=C:\\Users\\pburgr\\AppData\\Local\\Google\\Chrome\\User Data");
driver = new ChromeDriver(options);
driver.manage().window().maximize();}
#Before
public void setUp() {}
#After
public void tearDown() {}
#AfterClass
public static void tearDownClass() {driver.close();driver.quit();}
#Test
public void avi() throws InterruptedException {
driver.get("http://www.gcrit.com/build3/admin/");
wait5s.until(ExpectedConditions.elementToBeClickable(By.name("username"))).sendKeys("admin");
driver.findElement(By.name("password")).sendKeys("admin#123");
driver.findElement(By.id("tdb1")).click();
// wait to resolve the click before checking url
Thread.sleep(1000);
String url = driver.getCurrentUrl();
if (url.equals("http://www.gcrit.com/build3/admin/index.php")){
System.out.println("Successful");
}
else {
System.out.println("Unsuccessful");
}
}
}

Error at WebDriver

I have the following error:
Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkState(ZLjava/lang/String;Ljava/lang/Object;)V
at org.openqa.selenium.remote.service.DriverService.checkExecutable(DriverService.java:136)
at org.openqa.selenium.firefox.GeckoDriverService.access$000(GeckoDriverService.java:41)
at org.openqa.selenium.firefox.GeckoDriverService$Builder.usingFirefoxBinary(GeckoDriverService.java:134)
at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:155)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:120)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:98)
at automationFramework.SecondTest.main(SecondTest.java:20)
my code:
package automationFramework;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import junit.runner.Version;
public class SecondTest {
public static void main(String[] args) throws Exception {
System.out.println("JUnit version is: " + Version.id());
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
FirefoxDriver driver = new FirefoxDriver();
//System.setProperty("webdriver.gecko.driver", "C:\\\\Program Files\\Firefox Developer Edition\\firefox.exe");
System.setProperty("webdriver.gecko.driver", "E:\\\\Projects\\WebDriver\\geckodriver.exe");
driver.get("http://www.google.com");
}
}
This looks like a problem with Guava libraries. If you use maven to manage your dependencies, set a Guava version manually like this:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>22.0</version>
</dependency>
Also, remove the \\ part from your system property. It should only be \\

Selenium - org.openqa.selenium.chrome.ChromeDriver cannot be cast to com.thoughtworks.selenium.Selenium

I'm using flex-ui-selenium to automate my flex application, which adds 2 numbers and displays the result in an alert box.
Below is my selenium code:
package com.selenium.testcases;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import com.thoughtworks.selenium.FlexUISelenium;
import com.thoughtworks.selenium.Selenium;
public class FlashSeleniumtest {
#SuppressWarnings("deprecation")
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\dev\\HydFramework\\Hyd\\HybridFrameWork\\jarsForAnt\\chromedriver.exe");
String BASE_URL = "C:\\Program Files\\Apache Software Foundation\\Tomcat 7.0\\webapps\\FlexDemo\\SeleniumTest-debug\\SeleniumTest.html";
WebDriver driver = new ChromeDriver();
//FlashObjectWebDriver driver1 = new FlashObjectWebDriver((FlashObjectWebDriver) driver,"SeleniumTest");
driver.get(BASE_URL);
driver.manage().window().maximize();
FlexUISelenium flexUITester;
flexUITester = new FlexUISelenium((Selenium) driver, "SeleniumTest");
flexUITester.type("100").at("first");
flexUITester.type("200").at("second");
flexUITester.click("Sum");
}
}
When I execute this code, I get the following exception:
"org.openqa.selenium.chrome.ChromeDriver cannot be cast to com.thoughtworks.selenium.Selenium".
Please help.

Taking webpages snapshot using selenium server in java

I need to take snapshot of webpage and for that I am using selenium RC (this is good choice right ? )with eclipse for java language . I am using it as JUnit test case . Here is my code.
package com.example.tests;
import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.regex.Pattern;
#SuppressWarnings("deprecation")
public class mainClassTest extends SeleneseTestCase {
#Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.gmail.com/");
//water= new DefaultSelenium("localhost", 4444, "*firefox", "http://www.gmail.com/");
selenium.start();
}
#Test
public void testFinalSelenium() throws Exception {
selenium.windowMaximize();
selenium.open("/");
selenium.waitForPageToLoad("30000");
System.out.println("laoded\n");
// selenium.wait();
selenium.captureScreenshot("C:\\test\\simpleQuora.png");
selenium.captureEntirePageScreenshot("C:\\test\\CompleteQuora.png", "");
}
#After
public void tearDown() throws Exception {
selenium.stop();
}
}
And it's working fine but If i have to take snapshot of multiple URLs, then what are the ways to do that ?
Can we do it without using it as JUnit test case and using selenium in main function ?
Because if I try to run this code :
package com.example.tests;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
public class MainClass {
void func(String url, String file)
{
Selenium selenium = new DefaultSelenium("localhost", 4444, "*firefox", url);
selenium.start();
selenium.windowMaximize();
selenium.open("/");
selenium.waitForPageToLoad("30000");
System.out.println("laoded\n");
// selenium.wait();
String file1= "C:\\test\\"+file+".png";
String file2= "C:\\test\\"+file+"2.png";
selenium.captureScreenshot(file1);
selenium.captureEntirePageScreenshot(file2, "");
selenium.stop();
}
public static void main(String[] args)
{
MainClass demo = new MainClass();
demo.func("www.facebook.com","face");
//demo.func("www.reddit.com","reddit");
}
}
I got this error.(Although I have started server from cmd).
demo.func("www.facebook.com","face");
changes to
demo.func("http://www.facebook.com","face");

Categories