2

I want to use: Login_Page Login = PageFactory.initElements(driver, Login_Page.class); in a unique way in all steps. When I use it for each step I have no problems, but on the contrary Java shows me the error: " Value driver is always 'null'".

I would also like to replace Thread.sleep (2000); for a better solution.enter image description here

Here is my Code:

package stepdefs;

import Pages.Login_Page;
import cucumber.api.java.pt.Dado;
import cucumber.api.java.pt.Entao;
import cucumber.api.java.pt.Quando;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.PageFactory;

import java.util.concurrent.TimeUnit;

public class StepDefinitions {

      WebDriver driver;
      Login_Page Login = PageFactory.initElements(driver, Login_Page.class);

      @Dado("^que que estou na pagina principal do Gmail\\.$")
    public void que_que_estou_na_pagina_principal_do_Gmail () throws Exception
    {
        System.setProperty("webdriver.chrome.driver", "C:\\browsers\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.navigate().to("https://www.gmail.com");
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }

    @Quando("^forneco as credenciais validas\\.$")
    public void forneco_as_credenciais_validas () throws Exception {
      // Login_Page Login = PageFactory.initElements(driver, Login_Page.class);
        Login.setEmail("rbkamontana@gmail.com");
        Login.ClickOnNextButton();
        Thread.sleep(2000);
        Login.setSenha("automation10");
        Thread.sleep(3000);
        Login.ClickOnEnterButton();
        driver.manage().window().maximize();
        Thread.sleep(3000);
    }

    @Entao("^posso ver que estou logado\\.$")
    public void posso_ver_que_estou_logado () throws Exception {
        driver.findElement(By.xpath("//*[@id=\"gb\"]/div[2]/div[3]/div[1]/div[2]/div/a/span")).click();
        String stringAtual = driver.findElement(By.xpath("//*[@id=\"gb\"]/div[2]/div[4]/div[1]/div[2]/div[1]")).getText();
        String StringEsperada = "Rebeka Montana";
        Assert.assertTrue(stringAtual.contains(StringEsperada));
        //driver.quit();
    }
}
ChristianYami
  • 586
  • 1
  • 9
  • 17

2 Answers2

0

Instead of Thread.sleep you can use selenium WebDriverWait function by a Locator.

Ref: https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/WebDriverWait.html

void waitForElement(By Locator){
    WebDriverWait myWait = new WebDriverWait(myDriver, 20);
    myWait.until(ExpectedConditions.visibilityOfElementLocated(Locator));
}

Try initiating this inside of a constructor instead:

 public StepDefinitions(){
 driver = new ChromeDriver();
 Login  = PageFactory.initElements(driver, Login_Page.class);
 }
RRIL97
  • 858
  • 4
  • 9
  • Unfortunately it didn't work .. public StepDefinitions(){ Login = PageFactory.initElements(driver, Login_Page.class); } – Rebeka Montana Mar 13 '20 at 13:41
  • I want to use: Login_Page Login = PageFactory.initElements(driver, Login_Page.class); in a unique way in all steps. When I use it for each step I have no problems, but on the contrary Java shows me the error: " Value driver is always 'null'". – Rebeka Montana Mar 13 '20 at 14:11
0

An instance method is initializing the driver field at runtime, which explains why things work inside the step definition method. The solution is deceptively simple: create a getter method for the page model:

public class StepDefinitions {
    WebDriver driver;
    Login_Page login;

    private Login_Page getLoginPage() {
        if (login == null) {
            login = PageFactory.initElements(driver, Login_Page.class);
        }

        return login;
    }

    @Quando("^forneco as credenciais validas\\.$")
    public void forneco_as_credenciais_validas () throws Exception {
        Login_Page login = getLoginPage();

        login.setEmail("rbkamontana@gmail.com");
        login.ClickOnNextButton();
        Thread.sleep(2000);
        login.setSenha("automation10");
        Thread.sleep(3000);
        login.ClickOnEnterButton();
        driver.manage().window().maximize();
        Thread.sleep(3000);
    }
Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92