Feature file:
Feature: test of a webpage
@desktop
Scenario: Test landing page
Given Customer is on landing page
This is the step definition file:
import io.cucumber.java.Before;
import io.cucumber.java.BeforeAll;
import io.cucumber.java.Scenario;
import io.cucumber.java.en.And;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import org.assertj.core.api.Assertions;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.devtools.DevTools;
import pages.Landing Page;
import utils.BasePage;
import utils.ConfigureDriver;
import java.util.*;
import java.util.concurrent.TimeUnit;
import static utils.BasePage.driver;
public class LandingPageStepDefs {
LandingPage landingPage = new LandingPage(driver);
ScenarioContext scenarioContext = new ScenarioContext();
ConfigureDriver configureDriver = new ConfigureDriver();
@Given("Customer is on landing page")
public void customer_is_on_landing_page () {
landingPage.clickConsent();
}
@Before("not @mobile")
public void beforeDesktop(Scenario scenario) {
configureDriver.configureDesktop();
}
This is the LandingPage:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import utils.BasePage;
public class LandingPage extends BasePage {
public LandingPage (WebDriver driver) {
super(driver);
}
public void clickConsent() {
driver.findElement(By.xpath("//*[@id=\"cookie-terms\"]/div/div/button/i")).click();
}
}
This is the BasePage:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
import java.util.Arrays;
public class BasePage {
public static WebDriver driver;
public BasePage(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
public static WebDriver getDriver() {
return driver;
}
}
This is ConfigureDriver Page
package utils;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.util.concurrent.TimeUnit;
public class ConfigureDriver {
public void configureDesktop() {
ChromeOptions chromeOptions = new ChromeOptions();
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver(chromeOptions);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("https://www.page.com/");
}
}
There is null pointer exception in step def here. Could you tell how to get rid of it? Thanks in advance. landingPage.clickConsent();
You have to inherit the BasePage
class from ConfigureDriver
class, also you have to modify the WebDriver initialization line:
public class ConfigureDriver extends BasePage {
public ConfigureDriver(WebDriver driver) {
super(driver);
}
public void configureDesktop() {
ChromeOptions chromeOptions = new ChromeOptions();
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver(chromeOptions); // modified this line
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(15));
driver.manage().window().maximize();
driver.get("https://www.page.com/");
}
}
Next time, post your question in brief and clear, also do the correct indentation.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.