I m trying to run a small test program with Extent Reports but getting Null Pointer Exception. If i am running this code, without using Extent Report functionality it's working fine. Kindly help.
Below is my code-
package selenium.ExtentReports;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.reporter.ExtentSparkReporter;
import io.github.bonigarcia.wdm.WebDriverManager;
public class ExtentReportDemo {
ExtentReports extent;
WebDriver driver;
#BeforeTest
public void config() {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
String path = System.getProperty("user.dir") + "//Reports//index.html";
ExtentSparkReporter reporter = new ExtentSparkReporter(path);
reporter.config().setReportName("Web Automation Results");
reporter.config().setDocumentTitle("Test Results");
ExtentReports extent = new ExtentReports();
extent.attachReporter(reporter);
extent.setSystemInfo("Tester", "Gaurav Singh");
}
#Test
public void initialDemo() {
extent.createTest("Initial Demo");
driver.get("https://rahulshettyacademy.com");
System.out.println(driver.getTitle());
extent.flush();
}
}
Error which i am getting
FAILED: initialDemo
java.lang.NullPointerException
at selenium.ExtentReports.ExtentReportDemo.initialDemo(ExtentReportDemo.java:38)
I have just instantiate the extent variable outside the clas. And it's working for me now.
ExtentReports extent = new ExtentReports();
ExtentReports extent = new ExtentReports();
Don't initialize in #BeforeTest methods once you declared it Publicly. Instead Use :
extent = new ExtentReports();
Related
I'm using Selenium and TestNG for the first time and I've been trying to search an element by its ID but I keep getting an "Cannot instantiate class" error. This is my code:
import org.testng.annotations.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.*;
public class NewTesting {
WebDriver driver = new FirefoxDriver();
#BeforeTest
public void setUp() {
driver.get("http://book.theautomatedtester.co.uk/chapter1");
}
#AfterTest
public void tearDown() {
driver.quit();
}
#Test
public void testExample() {
WebElement element = driver.findElement(By.id("verifybutton"));
}
}
Maybe I missed installing something? I installed the TestNG plug-in for eclipse and added the WebDriver JAR files, do I need to do more?
I tried following multiple tutorials but I keep getting errors, I hope someone can help. Thanks in advance!
EDIT:
I now have this:
public class NewTest {
private WebDriver driver;
#BeforeTest
public void setUp() {
System.setProperty("webdriver.gecko.driver","C:\\Program Files\\Selenium\\FirefoxDriver\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://book.theautomatedtester.co.uk/chapter1");
}
#AfterTest
public void tearDown() {
driver.quit();
}
#Test
public void testExample() {
WebElement element = driver.findElement(By.id("verifybutton"));
}
}
It does open the website now but I'm getting a nullpointer exception now:
FAILED CONFIGURATION: #AfterTest tearDown
java.lang.NullPointerException
at NewTest.tearDown(NewTest.java:21)
Replace this set of imports:
import org.testng.annotations.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.*;
With:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.testng.annotations.AfterTest;
Additionally, you have to download the required format of GeckoDriver executable from mozilla/geckodriver, extract the binary and then initialize the FirefoxDriver.
Your effective code block will be:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.testng.annotations.AfterTest;
public class NewTesting {
WebDriver driver;
#BeforeTest
public void setUp() {
System.setProperty("webdriver.gecko.driver","C:\\path\\to\\geckodriver.exe");
driver = new FirefoxDriver();
driver.get("http://book.theautomatedtester.co.uk/chapter1");
}
#AfterTest
public void tearDown() {
driver.quit();
}
#Test
public void testExample() {
WebElement element = driver.findElement(By.id("verifybutton"));
}
}
If you're on windows, this previous question may be some help to you.
It mentions that you can download geckodriver, and then initialize your FirefoxDriver like this:
System.setProperty("webdriver.gecko.driver","G:\\Selenium\\Firefox driver\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
I was trying Selenium automation testing in https://www.yatra.com/etw-desktop/ . My objective was to click an Image Button named 'Asia' which will redirect to another page (Images attached). I copied the full XPath and tried but I am getting a NullPointerException. Please give some suggestions since I didn't find anything wrong with my code.
package com.stackroute.SeleniumProject;
import java.util.concurrent.TimeUnit;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.FindBy;
/**
* JUnit project.
*/
public class Yatra {
public static WebDriver driver = null;
#FindBy(xpath = "/html/body/my-app//app-drawer-layout/app-header-layout/iron-pages/my-home//div/div/div/div/paper-material[2]/div[1]/div/a[2]/div[4]")
WebElement Asia;
#BeforeClass
public static void setup() {
String chromePath = System.getProperty("user.dir") + "/lib/chromedriver.exe";// directory of chrome driver
System.setProperty("webdriver.chrome.driver", chromePath);
driver = new ChromeDriver();
}
#AfterClass
public static void close() throws InterruptedException {
driver.close();
}
#Test
public void test1() throws InterruptedException {
driver.manage().window().maximize();
driver.get("https://www.yatra.com/etw-desktop/");
driver.manage().timeouts().implicitlyWait(4000, TimeUnit.MILLISECONDS);
Thread.sleep(5000);
// the error is in the below line Asia.click()
Asia.click();
driver.manage().timeouts().implicitlyWait(5000, TimeUnit.MILLISECONDS);
Thread.sleep(2000);
Assert.assertEquals("page not found", "https://www.yatra.com/etw-desktop/city-list", driver.getCurrentUrl());
}
}
Image showing the element to be clicked
Image of web page after click operation
You are using PageFactory(from page object model), so you will need to init() the webelements.
For this you will need to import
org.openqa.selenium.support.PageFactory;
An before starting the test, you will need to initialise the elements:
PageFactory.initElements(driver, this) // where you pass the driver and this class to know which webelements to start.
You can take a look here to understand the approach and another POM framework easier to understand TUTORIAL
I'm using the Boni Garcia 'webdrivermanager'.
https://github.com/bonigarcia/webdrivermanager/blob/master/README.md
My issue:
I'm not able to 'get' my initialized driver properties from 'BaseSwag' to 'home.page' in order to launch Chrome and go to the desired URL. Here is my set up as follows. What can I do to fix this?
src/main/java/swaglogin/BaseSwag
package swaggerLogin;
import io.github.bonigarcia.wdm.WebDriverManager;
import io.github.bonigarcia.wdm.managers.ChromeDriverManager;
import io.github.bonigarcia.wdm.managers.FirefoxDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class BaseSwag {
public WebDriver driver;
public Properties prop;
public WebDriverManager initializeDriver() throws IOException {
// Create global property file
prop = new Properties();
FileInputStream fis = new FileInputStream(
"//Users/rad/WebTest/src/main/resources/data.properties");
prop.load(fis);
String browserName = prop.getProperty("browser");
System.out.println(browserName);
if (browserName.equals("chrome")) {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
} else if (browserName.equals("firefox")) {
WebDriverManager.firefoxdriver().setup();
driver = new FirefoxDriver();
}
return driver;
}
}
resources/data.properties
url = http://qaclickacademy.com/
apiKey = 1234563333random
browser = chrome
test/java/home.page
package home.page;
import java.io.IOException;
import org.testng.annotations.BeforeTest;
import swaggerLogin.BaseSwag;
public class SwaggerLoginDev extends BaseSwag {
#BeforeTest
public void initialize() throws IOException {
driver = initializeDriver();
driver.get(prop.getProperty("url"));
}
}
Stack Trace
Error:(32, 12) java: cannot find symbol
symbol: method driver()
location: class resources.Base
Your driver variable should be an instance of WebDriver, not WebDriverManager.
You can consider WebDriverManager as utility class, that only manage (download, setup, etc...) your drivers for different browsers. Once you call .setup() method for the desired type of browser, you can create an instance of it:
public class BaseSwag {
public WebDriver driver;
public Properties prop;
public WebDriver initializeDriver() throws IOException {
// Create global property file
prop = new Properties();
FileInputStream fis = new FileInputStream(
"//Users/rad/WebTest/src/main/resources/data.properties");
prop.load(fis);
String browserName = prop.getProperty("browser");
System.out.println(browserName);
if (browserName.equals("chrome")) {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
} else if (browserName.equals("firefox")) {
WebDriverManager.firefoxdriver().setup();
driver = new FirefoxDriver();
}
return driver;
}
}
Now, you should be able to call .get(...) method on your driver.
I'm not been able to import a class file from selenium 3.7.1 , although I have this class in the refrence library(which I have configured in the build path)
Please assist.
Below is a snapshot of my IDE where I'm trying to do the run the code
package mrLogin;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions; //Getting exception in this line
public class Login {
public static String driverPath = "C:/auto/Mass_Retail_Auto/bin/mrLogin/";
public static WebDriver driver;
public static ChromeOptions options;
public static void main(String[] args) {
try{
//options.AddExtension("C:/auto/Mass_Retail_Auto/bin/mrLogin/modheader
2.1.2.crx");
System.out.println("launching chrome browser");
System.setProperty("webdriver.chrome.driver",
driverPath+"chromedriver.exe");
driver = new ChromeDriver();
driver.navigate().to("http://google.com");
}
catch(Exception e ){
e.printStackTrace();
}
finally {
driver.close();
System.out.println("Browser closed!!!");
}
}
}
Use Selenium 3.0 or till 3.4 version which is the most stable one. You won't be facing any problem with those versions. My suggestion would be go for 3.0 version.
I am trying to run this selenium test code from Browserstack but I cannot get pass the error I am getting.
Code:
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.net.URL;
public class JavaSample {
public static final String USERNAME = "username";
public static final String AUTOMATE_KEY = "key";
public static final String URL = "https://" + USERNAME + ":" + AUTOMATE_KEY + "#hub-cloud.browserstack.com/wd/hub";
public static void main(String[] args) throws Exception {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("browser", "IE");
caps.setCapability("browser_version", "7.0");
caps.setCapability("os", "Windows");
caps.setCapability("os_version", "XP");
caps.setCapability("browserstack.debug", "true");
WebDriver driver = new RemoteWebDriver(new URL(URL), caps);
driver.get("http://www.google.com");
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("BrowserStack");
element.submit();
System.out.println(driver.getTitle());
driver.quit();
}
}
The error I am getting is
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The type java.lang.CharSequence cannot be resolved. It is indirectly referenced from required .class files
The method sendKeys(CharSequence...) from the type WebElement refers to the missing type CharSequence
at mypackage.JavaSample.main(JavaSample.java:30)
I have the selenium library and java library on the project. I am using eclipse.
A similar issue was reported here- Error when using sendKeys() with Selenium WebDriver Java.lang.CharSequence cannot be resolved. Can you check if it is something similar?