Error when executing testng.xml using pagefactory - selenium-webdriver

I am running 2 classes using testNG.First class is running successfully but second class is failing with error:
"org.testng.TestNGException:Can't invoke public void TestScripts.NewAccountCreation.AccountCreation1(): either make it static or add a no-args constructor to your class"
If I add non argument constructor I am getting null pointer exception.
I am using Pagefactory to design my test cases.
Eclipse version:kepler
TestnG:6.9.9
Chrome version: 57.0.2987.133
Chrome driver: 2.27
Any suggestions would be greatly appreciated.
Please find the below code:
Page factory Code for Login Page:
/**
*
*/
package Pages;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import Pages.LoginPage;
public class LoginPage {
WebDriver driver;
public LoginPage(WebDriver ldriver)
{
this.driver = ldriver;
}
#FindBy(xpath = "//input[contains(#id,'username-inputEl')]")
public WebElement username;
#FindBy(xpath = "//input[contains(#id,'password-inputEl')]")
public WebElement password;
#FindBy(xpath = "//span[contains(#id,'submit-btnInnerEl')]")
public WebElement LoginButton;
// Methods to perform actions
public void pCLogin(String Username, String Password, WebDriver driver)
{
username.sendKeys(Username);
password.sendKeys(Password);
LoginButton.click();
}
}
Page Factory code for Account creation page:
package Pages;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Accounttab {
WebDriver driver;
public Accounttab(WebDriver ldriver)
{
this.driver = ldriver;
}
#FindBy(xpath = "//span[#id='TabBar:AccountTab-btnWrap']")
public WebElement accountDropDown;
#FindBy(xpath = "//span[contains(#id,'AccountTab_NewAccount-textEl')]")
public WebElement NewAccount_Button;
#FindBy(xpath = "//textarea[contains(#id,'GlobalContactNameInputSet:Name-inputEl')]")
public WebElement Companyname;
#FindBy(xpath = "//input[contains(#id,'OfficialIDDV_Input-inputEl')]")
public WebElement FEINNumber;
#FindBy(xpath = "//input[contains(#id,'GlobalAddressInputSet:City-inputEl')]")
public WebElement City;
#FindBy(xpath = "//input[contains(#id,'GlobalAddressInputSet:PostalCode-inputEl')]")
public WebElement Zipcode;
#FindBy(xpath = "//a[contains(#id,'SearchLinksInputSet:Search')]")
public WebElement AccountsearchButton;
#FindBy(xpath = "//span[#class='x-btn-inner x-btn-inner-center'][contains(#id,'NewAccountButton-btnInnerEl')]")
public WebElement CreateNewButton;
#FindBy(css = "input[id='TabBar:AccountTab:AccountTab_AccountNumberSearchItem-inputEl']")
public WebElement AccountSearch;
#FindBy(xpath = "//tbody[contains(#id,'gridview')]//tr[1]//td[2]")
public WebElement SelectAccountNumber;
public void accountMouseHover(WebDriver driver) {
WebDriverWait wait1 = new WebDriverWait(driver, 10);
wait1.until(ExpectedConditions.visibilityOf(accountDropDown));
Actions builder2 = new Actions(driver);
builder2.moveToElement(accountDropDown).moveByOffset(50, 0).click()
.build().perform();
System.out.println("Dropdown is opened");
}
public void accountSearch(String AccountName, WebDriver driver)
{
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions .visibilityOf(NewAccount_Button));
driver.manage().window().maximize();
Actions builder1 = new Actions(driver);
builder1.moveToElement(NewAccount_Button).click().build().perform();
System.out.println("Clicked on New Accounr Button");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Companyname.sendKeys(AccountName);
try {
AccountsearchButton.click();
} catch (Exception e) {
System.out
.println("Please enter one of the minimum required fields: Company Name, FEIN"
+ e);
throw (e);
}
wait.until(ExpectedConditions .visibilityOf(CreateNewButton));
CreateNewButton.click();
}
}
Test Scripts1:
LoginClass:
**
package TestScripts;
import java.util.concurrent.TimeUnit;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import Pages.LoginPage;
import Utility.Configreader;
import Utility.GenericMethods;
public class Login {
WebDriver driver;
LoginPage Login_page;
#BeforeSuite
public void setUp() {
Configreader cr = new Configreader(
"H://Selenium//Selenium_ODSRegression//TestData//config.properties");
driver = GenericMethods.startBrowser("Chrome", cr.getURL());
Login_page = PageFactory.initElements(driver, LoginPage.class);
}
#Test
public void PClogin() {
Login_page.pCLogin("su", "gw", driver);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
}
**
Test Script2:
AccountCreation class:
package TestScripts;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.PageFactory;
import Pages.AccountFileSummary;
import Pages.Accounttab;
import Pages.CreateNewAccount;
public class NewAccountCreation {
WebDriver driver;
Accounttab Account_tab1;
CreateNewAccount NewAccount1;
AccountFileSummary AFS1;
public NewAccountCreation(WebDriver ldriver)
{
this.driver = ldriver;
Account_tab1 = PageFactory.initElements(driver, Accounttab.class);
}
#Test
public void AccountCreation1() {
Account_tab1.accountMouseHover(driver);
Account_tab1.accountSearch("WebDriver_Test1", driver);
}
}
driver class:
package Utility;
import java.io.File;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.ie.InternetExplorerDriver;
import Utility.Configreader;
public class GenericMethods {
public static WebDriver driver = null;
static Configreader cr = new Configreader(
"H://Selenium//Selenium_ODSRegression//TestData//config.properties");
public static WebDriver startBrowser(String browsername, String URL)
{
if (browsername.equalsIgnoreCase("Chrome"))
{
System.setProperty("webdriver.chrome.driver",
cr.getChromeDriverPath());
ChromeOptions options = new ChromeOptions();
options.addArguments("test-type");
options.addArguments("start-maximized");
options.addArguments("--js-flags=--expose-gc");
options.addArguments("--enable-precise-memory-info");
options.addArguments("--disable-popup-blocking");
options.addArguments("--disable-default-apps");
options.addArguments("test-type=browser");
options.addArguments("disable-infobars");
driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
else if (browsername.equalsIgnoreCase("IE"))
{
System.setProperty("webdriver.ie.driver", cr.getIEDriverPath());
driver = new InternetExplorerDriver();
driver.manage().window().maximize();
}
driver.get(URL);
return driver;
}
}
Property file:
PCURL = http://biltipolicycenter.thehartford.com/pc/PolicyCenter.do
ChromeBrowserPath = C://Selenium//ChromeDriver 2.27//chromedriver.exe
IEBrowserPath = C://Selenium//IEDriverServer x86 2.53//IEDriverServer.exe
TestNG.XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Test">
<classes>
<class name="TestScripts.Login"/>
<class name="TestScripts.NewAccountCreation"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->

NewAccountCreation is a test class because it contains test method(s) and because you declare it as a class test in the testng.xml.
So, it means TestNG must be able to create an instance of the class and that's why TestNG is complaining.
You have 2 options:
Using a factory to explain to TestNG how to create test classes
(best option IMO)
Using a static shared driver instance (Login#driver is not
yet static)
BTW, your organization of classes looks a bit dirty and you should rework them a bit.

Related

Test annotation code is not working, just BeforeMethod is working

My code is working until #BeforeMethod and it is opening the URL, but what I'm typing after #Test is not working. It is not typing credentials on the login page, hence Test failed.
package com.test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
// import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class Great {
WebDriver driver;
#BeforeMethod
public void setup()
{
System.setProperty("webdriver.chrome.driver", "C:\\Program Files
(x86)\\Google\\Chrome\\Application\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String baseurl = "https://getfieldforce.com/dishqa";
driver.get(baseurl);
// Actions act = new Actions(driver);
}
#Test (priority=0)
public void login()
{
System.out.println("Login process starts");
driver.findElement(By.id("email")).sendKeys("123#p.com");
driver.findElement(By.id("password")).sendKeys("123456");
driver.findElement(By.id("cta")).click();
System.out.println("Login Sucessfully");
}
Removing webdriver from this line fixed the issue; WebDriver driver = new ChromeDriver();
Thanks for looking into this :-)

How to define actions in page object modeling

I am newbee to selenium. I was trying to access menu->sub-menu in PageObject Modeling.
My requirement is to click on Menu item and sub-menu item which appears dynamically.
I was going through StackOverflow and in one of the resolutions it was mentioned to write Actions as
Actions actions = new Actions(driver);
actions.moveToElement(currentOpenings).build().perform();
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.elementToBeClickable(ausJobsSubmenu));
However i can't pass driver in constructor. if I pass constructor i am getting problem while running this script. The error is below
FAILED: checkLinks java.lang.Error: Unresolved compilation problem:
The method HomePageLinksClick(WebDriver) in the type HomePageLinksTest is not applicable for the arguments ()
If I include WebDriver driver in constructor as a parameter, my page class is not accepting that as parameter. Can anyone help?
Page Class
package au.com.sreetechconsulting.Pages;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import au.com.sreetechconsulting.TestCases.HomePageLinksTest;
public class HomePage {
public WebDriver driver;
#BeforeTest
public void openBrowser() {
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://www.sreetechconsulting.com.au/");
}
#Test
public void checkLinks() {
HomePageLinksTest linkClick = new HomePageLinksTest(driver);
linkClick.HomePageLinksClick();
}
}
Test Page Class
package au.com.sreetechconsulting.TestCases;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class HomePageLinksTest {
#FindBy(css="#header > div.container > div > div.col-md-9.main-nav > nav > ul > li:nth-child(2) > a")
private WebElement currentOpenings;
#FindBy(css="#header > div.container > div > div.col-md-9.main-nav > nav > ul > li:nth-child(2) > ul > li:nth-child(1) > a")
private WebElement ausJobsSubmenu;
public HomePageLinksTest (WebDriver driver) {
PageFactory.initElements(driver, this);
}
public void HomePageLinksClick() {
//currentOpenings.click();
Actions actions = new Actions(driver);
actions.moveToElement(currentOpenings).build().perform();
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.elementToBeClickable(ausJobsSubmenu));
}
}
You can keep the driver as a class member from the constructor
public class HomePageLinksTest {
private WebDriver driver;
public HomePageLinksTest (WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
public void HomePageLinksClick() {
//currentOpenings.click();
Actions actions = new Actions(driver);
actions.moveToElement(currentOpenings).build().perform();
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.elementToBeClickable(ausJobsSubmenu));
}
}
And in the test use it like this
#Test
public void checkLinks() {
HomePageLinksTest linkClick = new HomePageLinksTest(driver);
linkClick.HomePageLinksClick();
}

how to call function/method form one class to another class in selenium webdriver using java

I want to call login and search method from two different class into the main class. I have written the two diff classes for login and search but when I am calling both method in class containg main method .it gives me an error as below>>
Exception in thread "main" java.lang.NullPointerException
at Test_package_Ted_baker.search.search_1(search.java:14)
at Test_package_Ted_baker.SHopping.main(SHopping.java:16)
Can someone please help me to find out solution about how to call methods from one class to another class. My code is as below>>
Main class
package Test_package_Ted_baker;
import org.openqa.selenium.WebDriver;
public class SHopping {
static WebDriver driver;
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
login_Ted_Baker obj=new login_Ted_Baker();
obj.login_to_Ted_Baker("ss3777010#gmail.com", "google#123");
System.out.println("Login sucessfully");
search obj1=new search();
obj1.search_1();
obj.user_logout();
System.out.println("User log out sucessfully");
}
}
Login class
package Test_package_Ted_baker;
import java.util.HashMap;
import java.util.Map;
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.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class login_Ted_Baker {
WebDriver driver;
public static void main (String [] args) throws InterruptedException
{
login_Ted_Baker obj=new login_Ted_Baker();
obj.login_to_Ted_Baker("ss3777010#gmail.com", "google#123");
System.out.println("Login Sucessfully");
obj.user_logout();
}
public void usename( String Username)
{
WebElement Username_TB=driver.findElement(By.name("j_username"));
Username_TB.sendKeys(Username);
}
public void password(String Password)
{
WebElement Password_TB=driver.findElement(By.name("j_password"));
Password_TB.sendKeys(Password);
}
public void submit()
{
driver.findElement(By.xpath(".//*[#id='command']/div/input ")).click();
}
public void login_Ted_Baker(WebDriver driver){
this.driver = driver;
}
public void user_logout() throws InterruptedException
{
Thread.sleep(1000);
WebElement Sign_in_link=driver.findElement(By.xpath(".//*[#id='page']/header/div/nav[1]/ul/li[3]/div/a"));
WebElement Sign_out_button=driver.findElement(By.xpath(".//*[#id='account']/ul/li[2]/a"));
Thread.sleep(1000);
Actions Act1=new Actions(driver);
Act1.moveToElement(Sign_in_link).click();
Act1.moveToElement(Sign_out_button).click().build().perform();
System.out.println("Logout sucessfully");
}
public void login_to_Ted_Baker(String Username,String Password) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver","C:\\Chrome\\chromedriver_win32\\chromedriver.exe");
//System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Jdk14Logger");
Thread.sleep(2000);
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("credentials_enable_service", false);
prefs.put("password_manager_enabled", false);
options.setExperimentalOption("prefs", prefs);
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
driver = new ChromeDriver(options);
try{
driver.get("http://www.tedbaker.com/");
WebDriverWait wait = new WebDriverWait(driver, 40);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("html/body/div[3]/div[2]/div/div/ul/li[1]/a")));
if(driver.findElement(By.xpath("html/body/div[3]/div[2]/div/div/ul/li[1]/a")).isDisplayed())
{
driver.findElement(By.xpath("html/body/div[3]/div[2]/div/div/ul/li[1]/a")).click();
}
driver.findElement(By.xpath(".//*[#id='page']/header/div/nav[1]/ul/li[3]/div/a")).click();
Thread.sleep(2000);
this.usename(Username);
this.password(Password);
this.submit();
}catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
searching class
package Test_package_Ted_baker;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
public class search {
WebDriver driver;
public void search_1() throws InterruptedException
{
WebElement Search_Item=driver.findElement(By.xpath("html/body/div[2]/header/div/nav[2]/section/ul[2]/li[1]/a/span[1]"));
Search_Item.click();
WebElement Seacrh_item_message=driver.findElement(By.id("search"));
WebElement Search_textbox=driver.findElement(By.xpath("html/body/div[2]/header/div/form/div[1]/ol/li[1]/input"));
Search_textbox.sendKeys("Watches");
driver.findElement(By.xpath("html/body/div[2]/header/div/form/div[1]/ol/li[2]/input")).click();
WebElement SearchResult_pagemessage=driver.findElement(By.xpath("html/body/div[2]/div/div/div[1]/div/h1"));
driver.findElement(By.xpath("html/body/div[2]/div/div/div[1]/div/div/div/span[1]")).click();
WebElement Item_Tobuy = driver.findElement(By.xpath("html/body/div[2]/div/div/div[2]/div[2]/div[1]/article/div[2]/header/div/h4/a"));
JavascriptExecutor jse1= (JavascriptExecutor)driver;
jse1.executeScript("window.scrollBy(0,400)", "");
Item_Tobuy.click();
driver.findElement(By.xpath("html/body/div[2]/div/div/div[2]/div[1]/section[2]/form/ol/li[2]/span/span/select")).click();
Select dropdown1=new Select(driver.findElement(By.id("qty")));
dropdown1.selectByVisibleText("1");
driver.findElement(By.id("qty")).click();
// driver.findElement(By.xpath("*[#classname='button add_to_cart major full_mobile colour_dark']/div[2]/div[1]/div/input[1]")).click();
// driver.findElement(By.xpath("//*[#id='add_to_cart_form']/div[2]/div[2]/a/span[1]/span[1]")).click();
driver.findElement(By.xpath("//input[contains(#class,'add_to_cart')]")).click();
Thread.sleep(1000);
Actions act=new Actions(driver);
WebElement My_cart_button=driver.findElement(By.xpath("html/body/div[2]/header/div/nav[2]/section/ul[2]/li[3]/a/span[2]"));
//WebElement My_cart_button=driver.findElement(By.xpath("//input[contains(#class,'My bag')]"));
WebElement View_bag_checkout_button=driver.findElement(By.xpath("html/body/div[2]/header/div/nav[2]/section/ul[2]/li[3]/div/ul/li/a"));
act.moveToElement(My_cart_button).moveToElement(View_bag_checkout_button).click().build().perform();
driver.findElement(By.xpath("html/body/div[2]/div/div/div/section/table/tbody/tr/td[2]/form/ul/li[2]/a")).click();
String Cart_empty_mesg=driver.findElement(By.xpath("html/body/div[1]/div/div/div/header/h1")).getText();
System.out.println(Cart_empty_mesg);
String Actual_cart_empty_msg="Your shopping bag is empty";
if(Cart_empty_mesg==Actual_cart_empty_msg)
{
System.out.println("Cart is empty, you can add product cart");
}
}
}
This might sound harsh, but first of all you should read about
Null Pointer Exception from this link.
How to debug from a StackTrace from this link.
Why using absolute xpath, or xpaths locators in general is a bad strategy from here.
From your stack trace,
Exception in thread "main" java.lang.NullPointerException at
Test_package_Ted_baker.search.search_1(search.java:14) at
Test_package_Ted_baker.SHopping.main(SHopping.java:16)
I see that your code in Search class
WebElement Search_Item=driver.findElement(By.xpath("html/body/div[2]/header/div/nav[2]/section/ul[2]/li[1]/a/span[1]"));
is not correct. I suspect your locator is not correct for this one. The element can easily be identified using an id attribute, as
driver.findElement(By.id("search"));
or , if you're using xpaths, then
driver.findElement(By.xpath("//*[#id="search"]"));
should do away the NPE - but not sure since you've used a lot of absolute xpaths - which is a horrible practise.

Null Pointer Exception using TestNG

I am trying to automate and have integrated Selenium with Appium and I am using Eclipse with TestNG for execution.
Now within a project I have a package with two classes; the first class is for login into the App and the second class is for logout
First class code -
package com.gma.test;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class LoginApp {
public AppiumDriver driver;
#BeforeSuite
public void beforeMethod() throws InterruptedException {
File app = new File("C:\\Users\\mc30058\\Downloads\\gma-QA-RELEASE-QRCode-07022015_v4.5.4028.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName","Android Emulator");
capabilities.setCapability("platformVersion", "4.4");
capabilities.setCapability("app", app.getAbsolutePath());
try {
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
} catch (MalformedURLException e) {
e.printStackTrace();
}
driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
driver.findElement(By.xpath("//android.widget.Button[#resource-id='com.mcdonalds.app:id/button_choose_closest']")).click();
driver.findElement(By.xpath("//android.widget.Button[#text='Continue']")).click();
driver.findElement(By.xpath("//android.widget.Button[#text='OK']")).click();
}
#Test (priority=1)
public void Login() {
driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
driver.findElement(By.xpath("//android.widget.TextView[#text='Sign In']")).click();
driver.findElement(By.xpath("//android.widget.EditText[#resource-id='com.mcdonalds.app:id/signin_edittext_email']")).sendKeys("anuj.shrivastava#us.mcd.com");
driver.findElement(By.xpath("//android.widget.EditText[#resource-id='com.mcdonalds.app:id/signin_edittext_password']")).sendKeys("Anujtest2");
currentTime();
driver.findElement(By.xpath("//android.widget.Button[#text='Sign In']")).click();
driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
driver.findElement(By.xpath("//android.widget.Button[#text='Yeah, count me in!']")).isDisplayed();
currentTime();
driver.findElement(By.xpath("//android.widget.Button[#text='No, thanks. I like to be out of the loop']")).click();
}
private void currentTime() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
System.out.println( sdf.format(cal.getTime()) );
}
}
Second class code:
package com.gma.test;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class Logout {
public AppiumDriver driver;
#Test (priority=3)
public void LogoutApp() {
driver.findElement(By.xpath("//android.widget.TextView[#content-desc='Navigation Button']")).click();
driver.findElement(By.xpath("//android.widget.TextView[#text='Sign Out']")).click();
driver.findElement(By.xpath("//android.widget.Button[#text='Sign Out']")).click();
}
}
Contents from Testng.xml
<suite name = "GMA Automation">
<test name = "GMA Automation">
<classes>
<class name = "com.gma.test.LoginApp"/>
<class name = "com.gma.test.Logout"/>
</classes>
</test>
</suite>
I receive Null Pointer exception after execution of first class. Appium server stops saying no more commands received. Please help.
You are getting NPE b'coz you have declared one more driver reference public AppiumDriver driver; in your Logout class.
And you are using this driver reference without instantiation.
In logout class you forget to instantiate your AppiumDriver driver.
try this:
public AppiumDriver driver;
driver = new AndroidDriver();

the method get(class <ReportClass>) is undefined for the type ExtentReport

i'm making selenium extent report but i'm getting error on - static final ExtentReports extrpt=ExtentReports.get(ReportClass.class);
after mouse hover on get i'm getting below info.
the method get(class ) is undefined for the type ExtentReports
it's my simple java project please tell me where am i doing mistake.
package DemoPacakge;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
import com.relevantcodes.extentreports.ExtentReports;
public class ReportClass {
// * ReportClass .class will become TheClassName.class
static final ExtentReports extrpt=ExtentReports.get(ReportClass.class);
public void test()
{
WebDriver driver =new FirefoxDriver();
driver.get("http://learn-automation.com/advance-selenium-reporting-with-screenshots/");
String tile=driver.getTitle();
Assert.assertTrue(tile.contains("learn"));
}
}
Please see the examples section: http://extentreports.relevantcodes.com/1x/docs.html#examples
There are a few errors such as, you are not initializing the report with a "file-path". You have not instructed Extent to start a test either. Try with the below code, it should work:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.LogStatus;
public class ReportClass {
static final ExtentReports extrpt = ExtentReports.get(ReportClass.class);
WebDriver driver;
#BeforeClass
public void beforeClass() {
extrpt.init("file-path.html", true);
extrpt.config().displayCallerClass(false);
}
#Test
public void test() {
extrpt.startTest("Test");
driver = new FirefoxDriver();
extrpt.log(LogStatus.INFO, "Starting FirefoxDriver..");
driver.get("http://learn-automation.com/advance-selenium-reporting-with-screenshots/");
extrpt.log(LogStatus.INFO, "Navigating to learn-automation.com..");
String title = driver.getTitle();
extrpt.log(LogStatus.INFO, "Title: " + title);
try {
Assert.assertTrue(title.contains("learn"));
extrpt.log(LogStatus.PASS, "Step Passed");
}
catch (AssertionError e) {
extrpt.log(LogStatus.FAIL, "<pre>" + e.getMessage() + "</pre>");
}
}
#AfterTest
public void afterTest() {
driver.quit();
extrpt.endTest();
}
}

Resources