Getting InstantiationException when creating factory for a page using appium - selenium-webdriver

I am running a test case using pagefactory method and have created an appium driver. I'm trying to initialising a page using pagefactory class like this:
The test class:
public class VerifyValidLogin {
#Test
public void CheckValidUser() throws MalformedURLException {
AppiumDriver driver = DeviceFactory.CreateDriver();
login login_page = PageFactory.initElements(driver, login.class);
}
}
DeviceFactory class:
public class DeviceFactory {
public static AppiumDriver<MobileElement> driver;
public static AppiumDriver CreateDriver() throws MalformedURLException {
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability( capabilityName: 'deviceName', value: 'Something');
...
URL url = new URL("http://127.0.0.1:4723/wd/hub");
driver = new AppiumDriver<MobileElement>(url,cap);
System.out.print("Application started");
return driver;
}
}
Login class has element locators:
public class login {
AppiumDriver driver;
public login(AppiumDriver ldriver)
{
this.driver=ldriver;
}
#FindBy(how = How.XPATH,using ="xpath");
MobileElement SignInButton;
}
But i'm not sure where i am doing wrong.
The error is
java.lang.RuntimeException: java.lang.InstantiationException: com.Demo.pages.login
at org.openqa.selenium.support.PageFactory.instantiatePage(PageFactory.java:134)
at org.openqa.selenium.support.PageFactory.initElements(PageFactory.java:64)
at com.Demo.Testcases.VerifyValidLogin.CheckValidUser(VerifyValidLogin.java:18)
...
I am new to the automation testing so i'm not able to properly understand the error. Let me know if you need more details.

Initialise elements in constructor
public login(AppiumDriver ldriver)
{
this.driver=ldriver;
PageFactory.initElements(ldriver,this);
}

Related

Getting null pointer exception while open the url in selenium automation [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 2 years ago.
Here, I am using page object model and I want to pass the driver to other classes.But i am getting null pointer exception while launch the website (driver.get("")).
This is my base class
public class BaseClass {
public WebDriver driver;
public Logger logger = Logger.getLogger(Common.class.getPackage().getName());
public void startBrowser() {
if(driver == null) {
System.setProperty("webdriver.chrome.driver", "desktop/chromedriver.exe");
driver = new ChromeDriver();
}
}
public void quitBrowser() {
driver.quit();
}
}
and then this is my runner class:
public class TestRunnerTestNG extends AbstractTestNGCucumberTests {
BaseClass a;
#BeforeClass
public void launch()
{
a = new BaseClass();
a.startBrowser();
}
#AfterClass
public void tearBrowser()
{
a.quitBrowser();
}
}
Here, I am starting the browser using Beforeclass annotation and quit the browser using afterClass annotation.
and the following class is my page Object class: and here I have the method for launch the url:
public class SignIn extends BaseClass {
public SignIn(WebDriver driver) {
this.driver = driver ;
PageFactory.initElements(driver, this);
}
//Locators
#FindBy(id = "email")
private WebElement user_Email;
#FindBy(id = "password")
private WebElement user_Password;
#FindBy(xpath = "//span[text()='Sign In']")
private WebElement signIn_Btn;
public void landing()
{
driver.get("https://***************"); <<<< Here I am getting the null pointer exception.
}
public void signInPageGUI()
{
boolean checkSignInTextGUI = waitElement(signInText);
Assert.assertTrue(checkSignInTextGUI);
boolean CheckEmailField = waitElement(user_Email);
Assert.assertTrue(CheckEmailField);
boolean checkPwdField = waitElement(user_Password);
Assert.assertTrue(checkPwdField);
}
private void emailField(String emailName) {
user_Email.sendKeys(emailName);
}
private void passwordField(String password) {
user_Password.sendKeys(password);
}
}
and the final code is my step definition class and this is place I am calling the code.
public class LoginPage {
WebDriver driver ;
#Given("user landed to the yoco URL {string}")
public void landedOnYoCo(String string) {
System.out.println("print the string" +string);
System.out.println("driver value is " );
SignIn logIN = new SignIn(driver);
logIN.landing();
}
}
Here, Only I am calling the landing method to launch the website.
and The error is:
java.lang.NullPointerException
at pageObject.SignIn.landing(SignIn.java:83)
at stepDefs.LoginPage.landedOnYoCo(LoginPage.java:32)
at ✽.user landed to the yoco URL "https://my.yocoboard.com"(file:///Users/vinoth/Git/YoCoAutomation/src/test/resources/logIN.feature:7)
Call startBrowser before calling landing to initialize driver.
public void landedOnYoCo(String string) {
System.out.println("print the string" +string);
System.out.println("driver value is " );
SignIn logIN = new SignIn(driver);
logIN.startBrowser();
logIN.landing();
}

How to handle multiple step definition file in Cucumber with Selenium

I have two step definition files and in future, there will be multiple, I am unable to execute my code for logout, its working fine for login and one step definition file, and it continuously open chrome browser.
I have created a framework using page factory, below is my code:
Login Page:
public class LoginPage {
WebDriver driver;
public LoginPage(WebDriver driver) {
//this.driver=driver;
PageFactory.initElements(driver, this);
}
#FindBy(how=How.ID,using="username")
public WebElement usernametexbox;
#FindBy(how=How.ID,using="pass")
public WebElement passwordtextbox;
#FindBy(how=How.ID,using="submit")
public WebElement signin;
#FindBy(how=How.XPATH,using="//button[#class='btn']")
public WebElement acceptbutton;
public void enter_username(String username) {
usernametexbox.clear();
usernametexbox.sendKeys(username);
usernametexbox.getText();
}
public void enter_password(String password) {
passwordtextbox.clear();
passwordtextbox.sendKeys(password);
}
public void clickToSigninbutton() {
signin.click();
}
public void clickToAcceptbutton() {
acceptbutton.click();
}
public void fill_LoginDetails() {
enter_username("abc");
enter_password("def45");
}
}
Logout Page:
public class LogoutPage {
WebDriver driver;
public LogoutPage(WebDriver driver) {
//this.driver=driver;
PageFactory.initElements(driver, this);
}
#FindBy(how=How.XPATH,using="//*[#class='icon']")
public WebElement chevron;
#FindBy(how=How.XPATH,using="//a[#class='logout-link']")
public WebElement logoutlink;
public void clickTochevron() {
chevron.click();
}
public void clickToLogoutLink() {
link.click();
}
}
Property file reader:
public class PropertiesFileReader {
public Properties getproperty() throws IOException {
FileInputStream inputstream=null;
Properties properties=new Properties();
try {
properties.load(new FileInputStream("resources/config.properties"));
}catch(Exception e) {
System.out.println("Exception " +e);
}
return properties;
}
}
Browser utility:
public class BrowserUtility {
public static WebDriver openBrowser(WebDriver driver, String browserName, String url) throws InterruptedException {
if(browserName.equals("chrome")) {
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver\\chromedriver.exe");
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get(url);
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
return driver;
}else if(browserName.equals("IE")) {
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver\\chromedriver.exe");
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get(url);
Thread.sleep(5000);
return driver;
}else if(browserName.equals("Firefox")) {
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver\\chromedriver.exe");
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get(url);
Thread.sleep(5000);
return driver;
}
return driver;
}
}
Login Stepdef:
public class StepDefinition {
public static WebDriver driver;
// public LoginPage loginpage;
// Properties properties=new Properties();
PropertiesFileReader obj=new PropertiesFileReader();
#Given("^Open browser and enter url$")
public void open_browser_and_enter_url() throws Throwable {
Properties properties=obj.getproperty();
driver= BrowserUtility.openBrowser(driver, properties.getProperty("browser.Name"), properties.getProperty("URL"));
}
#Then("^Enter username and password$")
public void enter_username_and_password() throws Throwable {
LoginPage loginpage=new LoginPage(driver);
loginpage.fill_LoginDetails();
}
#Then("^click on sign in button$")
public void click_on_sign_in_button() throws Throwable {
new LoginPage(driver).clickToSigninbutton();
System.out.println("Sign-In successfully");
}
#Then("^Terms and conditions page open and click on Accept button$")
public void terms_and_conditions_page_open_and_click_on_Accept_button() throws Throwable {
new LoginPage(driver).clickToAcceptbutton();
}
}
Logout stepdef:
public class Logoutstepdef {
public static WebDriver driver;
PropertiesFileReader obj=new PropertiesFileReader();
#Given("^Chevron near username$")
public void chevron_near_username() throws Throwable {
Properties properties=obj.getproperty();
driver= BrowserUtility.openBrowser(driver,
properties.getProperty("browser.Name"), properties.getProperty("URL"));
LogoutPage logoutpage=new LogoutPage(driver);
logoutpage.clickTochevron();
}
#Then("^click on chevron and it should get expands$")
public void click_on_chevron_and_it_should_get_expands() throws
Throwable {
System.out.println("when user click on checvron it should
further expands a window");
}
#Then("^click on Logout link$")
public void click_on_Logout_link() throws Throwable {
new LogoutPage(driver).clickToLogoutLink();
}
}
Expected Results: Application should get automated successfully for different step definition files and only one browser should get opened at a time.
Actual Results: I have two step definition file and in future there will be multiple, I am unable to execute my code for logout, its working fine for login and one step definition file, and it continuously open chrome browser.
Your Then step is creating another instance of the page, that's why you get multiple browsers open.
Try doing this in your Logoutstepdef class:
public class Logoutstepdef {
public static WebDriver driver;
PropertiesFileReader obj=new PropertiesFileReader();
private LogoutPage logoutpage;
.....//the rest remains the same, until:
#Then("^click on Logout link$")
public void click_on_Logout_link() throws Throwable {
logoutpage.clickToLogoutLink();
}
}
There are similar questions here and here.
i would suggest to try gherkin with qaf. With QAF you don't need to manage driver or don't need to worry about page. All you need to extend WebdriverTestPage to your page class and you are done. You also can have steps in Page class itself. For example, with qaf your logout page may look like below:
public class LogoutPage extends WebDriverBaseTestPage<WebDriverTestPage>{
#FindBy(locator="xpath=//*[#class='icon']")
public WebElement chevron;
#FindBy(locator="xpath=//a[#class='logout-link']")
public WebElement logoutlink;
public void clickTochevron() {
chevron.click();
}
//#Then("^click on Logout link$") //you can use cucumber or qaf step annotation
#QAFTestStep(description="click on Logout link")
public void clickToLogoutLink() {
link.click();
}
}
Browser management (opening/closing browser) is taken care by QAF so depending on your configuration (sequential/parallel) qaf will provide thread safe drive sessions.
Furthermore, with locator repository you can eliminate one layer of page class. There are in-built steps available you can directly use. For example:
logoutpage.propeties
chevron.icon.loc=xpath=//*[#class='icon']
logout.link.loc=xpath=//a[#class='logout-link']
in your BDD you can directly use in-built steps as below:
Scenario: name of scenario
Given ...
When ...
Then verify 'chevron.icon.loc' is present
And click on 'logout.link.loc'
To make your locator self descriptive, you can go one step ahead as below:
logout.link.loc=xpath={"locator":"//a[#class='logout-link']","desc":"logout button"}
The description will be used in reporting to make it more meaningful.
There are lots of other feature that are crucial for functional test automation.

Page Objects erro (java.lang.NullPointerException)

I did a basic project to training about Page Objects using selenium WebDriver with java and Junit. So, I make a page object class and Junit class too. I Make a call of method and pass the parameters to method but, the eclipse show a message that say: java.lang.NullPointerException
public class LogarBkoMaisPage {
static WebDriver driver;
By campoNome = By.id("matricula_I");
By campoSenha = By.id("senha_I");
By btnLogin = By.id("bt_entrar");
public LogarBkoMaisPage(WebDriver driver) {
this.driver = driver;
}
public void logar(String usuario, String senha) {
driver.findElement(campoNome).sendKeys(usuario);
driver.findElement(campoSenha).sendKeys(senha);
driver.findElement(btnLogin).click();
}
}
public class LogarBkoMaisTest {
static WebDriver driver;
#Before
public void setUp() throws Exception {
SelecionarNavegador nav = new SelecionarNavegador();
nav.iniciarNavegador("ie","http://10.5.9.45/BkoMais_Selenium/");
}
#Test
public void logarAplicacao() {
try {
LogarBkoMaisPage login = new LogarBkoMaisPage(driver);
login.logar("844502","Bcc201707");
}catch(Exception e) {
System.out.println("Mensagem de erro: " +e);
}
}
#After
public void tearDown() throws Exception {
}
}
public class SelecionarNavegador {
static WebDriver driver;
public static WebDriver iniciarNavegador(String nomeNavegador, String url) {
if(nomeNavegador.equalsIgnoreCase("firefox")) {
System.setProperty("webdriver.gecko.driver", "E:\\workspace_BCC_QA_BKOMAIS\\"
+ "FireFoxGeckodriver64\\geckodriver.exe");
driver = new FirefoxDriver();
}
else if(nomeNavegador.equalsIgnoreCase("chrome")){
System.setProperty("webdriver.chrome.driver", "E:\\workspace_BCC_QA_BKOMAIS"
+ "\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
}else if(nomeNavegador.equalsIgnoreCase("IE")) {
System.setProperty("webdriver.ie.driver", "E:\\workspace_BCC_QA_BKOMAIS"
+ "\\IE Plugin\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
}
driver.manage().window().maximize();
driver.get(url);
return driver;
}
}
Exception:
You are getting a null pointer exception because iniciarNavegador method inside SelecionarNavegador class is the one which initializes the driver and it returns the driver which has to be assigned to a vairable. You need to do this in your setUp()method
#Before
public void setUp() throws Exception {
SelecionarNavegador nav = new SelecionarNavegador();
driver=nav.iniciarNavegador("ie","http://10.5.9.45/BkoMais_Selenium/");
}

Selenium POM java.lang.NullPointerException

I am getting "java.lang.NullPointerException" when I am trying to execute my test case based on POM.
The class BrowserFactory lets me choose a browser, the class Flipkart_Login based on POM stores all the element of that particular page and has a method for Valid_Login()
and finally Test_Flipkart_Login class - calls the Valid_Login() method for executon but when I try to execute this class, I get java.lang.NullPointerException.
Kindly advise!
FAILED: Flipkart_Login_Test
java.lang.NullPointerException
at DataProviders.ConfigDataProvider.getURL(ConfigDataProvider.java:31)
at TestCases.Test_Flipkart_Login.Flipkart_Login_Test(Test_Flipkart_Login.java:19)
public class ConfigDataProvider
{
static Properties pro;
public ConfigDataProvider()
{
File src = new File("C:\\Data\\Bimlesh\\Flipkart_HybridFramework\\Flipkart.Hybrid.FrameworkComplete\\Configuration\\Config.Properties");
try
{
FileInputStream fis = new FileInputStream(src);
pro = new Properties();
pro.load(fis);
} catch (Exception e)
{
System.out.println("The Config exception is :"+e.getMessage());
}
}
public static String getURL()
{
String URL = pro.getProperty("URL");
return URL;
}
public static String ChromePath()
{
String Chrome = pro.getProperty("Chromepath");
return Chrome;
}
public static String IEPath()
{
String IE = pro.getProperty("IEpath");
return IE;
}
}
public class BrowserFactory
{
static WebDriver driver;
public static WebDriver getBrowser(String BrowserName)
{
if(BrowserName.equalsIgnoreCase("Firefox"))
{
driver = new FirefoxDriver();
}
else if(BrowserName.equalsIgnoreCase("Chrome"))
{
System.setProperty("webdriver.chrome.driver", ConfigDataProvider.ChromePath());
driver = new ChromeDriver();
}
else if(BrowserName.equalsIgnoreCase("IE"))
{
System.setProperty("webdriver.ie.driver", ConfigDataProvider.IEPath());
driver = new InternetExplorerDriver();
}
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
return driver;
}
public void ShutDown(WebDriver driver)
{
driver.quit();
}
}
public class Flipkart_Login
{
WebDriver driver;
public Flipkart_Login(WebDriver driver)
{
this.driver=driver;
}
#FindBy(xpath="//a[text()='Log In']") WebElement Login_Click;
#FindBy(xpath="//input[#class='_2zrpKA' and #type='text']") WebElement Enter_Email;
#FindBy(xpath="//input[#class='_2zrpKA _3v41xv' and #type='password']") WebElement Enter_Pass;
#FindBy(xpath="//button[#type='submit' and #class='_3zLR9i _1LctnI _36SmAs']") WebElement Login_Button;
public void Valid_Login()
{
Login_Click.click();
Enter_Email.sendKeys("xxx#gmail.com");
Enter_Pass.sendKeys("xxx");
Login_Button.click();
}
}
public class Test_Flipkart_Login
{
WebDriver driver;
#Test
public void Flipkart_Login_Test()
{
driver = BrowserFactory.getBrowser("Firefox");
driver.get(ConfigDataProvider.getURL());
Flipkart_Login page1 = PageFactory.initElements(driver, Flipkart_Login.class);
page1.Valid_Login();
}
}
You have initialized Properties pro in the constructor of the COnfigDataProider but you are using a static call to getURL method from your test class. Thus pro will be null and not initialized. Remove static call and use the constructor or make pro to static and initialize in static block.

How to get page object relative to another page object in Selenium WebDriver

I'm looking for solution how to get page object relative to another object in Page Object Model for selenium webdriver
Code of my test:
class StartPage {
WebDriver driver;
public HomePage(driver) {
this.driver = driver;
}
#FindBy(xpath="//div[#class='widget']//a[text()='link text']")
WebElement linkInWidget;
public void clickLink() {
linkInWidget.click();
return PageFactory.initElements(driver, NextPage.class);
}
}
Next page
class NextPage {
WebDriver driver;
public HomePage(driver) {
this.driver = driver;
}
#FindBy(xpath="//div[#class='widget']//input[#type='button']")
WebElement buttonInWidget;
#FindBy(id = "Index")
WebElement index;
public void clickButton() {
buttonInWidget.click();
return PageFactory.initElements(driver, NextPage.class);
}
public String getText() {
return index.getText();
}
}
Configuration class
public class ConfigureTest{
protected WebDriver driver;
protected String baseUrl;
protected StartPage startPage;
protected NextPage nextPage ;
#BeforeSuite
public void setUp() {
baseUrl = "http://webapp.com/";
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#AfterSuite
public void tearDown() throws Exception {
driver.quit();
}
}
And class of my test
public class SomeTest extends ConfigureTest {
#Test
public void testLinkAndButton() throws Exception {
startPage = PageFactory.initElements(driver, SomePage.class);
driver.get(baseUrl);
nextPage = startPage.clickLink();
nextPage.clickButton();
String data = nextPage.getText();
}
}
In both classes FirstPage and NextPage i find elements by xpath which has same first part //div[#class='widget'] it mean that all elements like buttons and links are under this widget and i have same xpath for widgets in my all pages
Problem: if only xpath of my widget will be changed i must make changes in all page objects of my test
Question: Is in any way how to improve my test for more flexibility with usage like:
page().get(Widget.class, "Widget name").get(Button.class, "Button name").click
Update: I resolve part of this problem in such way:
I create classes of my UI elements with get methods which can return objects of any class:
Class of Widget object
public class widget{
WebDriver driver;
public widget (WebDriver driver) {
this.driver = driver;
}
// Find a single element
#FindBy(xpath="//div[#class='tsf-p']")
WebElement linkInWidget;
public void click() {
linkInWidget.click();
}
public <T> T get(Class<T> expectedPage, String uiclass){
return PageFactory.initElements(driver, expectedPage);
}
}
Class of Button object
public class Button {
WebDriver driver;
public Button (WebDriver driver) {
this.driver = driver;
}
#FindBy(name="btnG")
WebElement button;
public void click() {
button.click();
}
public <T> T get(Class<T> expectedPage){
return PageFactory.initElements(driver, expectedPage);
}
}
Class of HomePage
public class HomePage {
WebDriver driver;
public HomePage(WebDriver driver) {
this.driver = driver;
}
#FindBy(xpath="//div[#class='widget']//a[text()='']")
WebElement linkInWidget;
public void click() {
linkInWidget.click();
}
public <T> T get(Class<T> expectedPage, String name){
return PageFactory.initElements(driver, expectedPage);
}
}
My test
public class searchTest {
WebDriver driver;
#BeforeTest
public void setup() {
driver = new FirefoxDriver();
driver.get("https://www.google.com.ua/");
}
#Test
public void testUI() {
HomePage homePage = PageFactory.initElements(driver, HomePage.class);
widget widget = PageFactory.initElements(driver, widget.class);
homePage.get(widget.class).get(Input.class).setValue("yahoo");
homePage.get(widget.class).get(Button.class).click();
}
}
And a result is that we can compose any object by using our classes
homePage.get(widget.class).get(Input.class).setValue("yahoo");
But how to get element by it name or number for example:
homePage.get(widget.class, "Name").get(Input.class, 1).setValue("yahoo");
I have a public repository here where I have implemented PageObject and PageFactory concept with TestNG. You are probably looking for a better way to inherit BaseClasse. The common methods should be placed in BaseClass and available to all PageObjects through inheritance. I have everything placed in GitHub and it's too broad to implement here.

Resources