Spring Boot 忽略方案步骤

wfypjpf4  于 2023-03-29  发布在  Spring
关注(0)|答案(1)|浏览(283)

I have created multiple login scenarios in one feature file. When I execute each scenario separately it's working fine without any errors. When I execute the login feature file only the first scenario is executed and other scenario steps are ignored.
WebDriver Library:

@Configuration
//@Profile("!remote")
public class WebDriverLibrary {
    @Value("${remote.url}")
    private URL remoteUrl;

    @Value("${browser}")
    private String browser;

    @Bean
    public WebDriver getChromeDriver(){
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("--remote-allow-origins=*");
        return new ChromeDriver(chromeOptions);
    }

BasePage:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;

public abstract class basePage {

    @Autowired
    private WebDriver webDriver;

    @PostConstruct
    public void InitPage(){
        PageFactory.initElements(webDriver, this);
    }
}

Login Feature File:

Feature: Login Feature
  This feature will deal with Login Functionality of the application

  Scenario: Login with Valid Credential
    Given User is on Landing Page
    And Navigate to Login Page
    And User enter email and Password
    And User click Login Button
    Then User should be navigated to Questionnaire form

  Scenario: Login with InValid Credential
    Given User is on Landing Page
    And Navigate to Login Page
    And User enter email and Password
    And User click Login Button
    Then User should get error message

  Scenario: Navigating to Login Page through SignUp Page
    Given User is on Landing Page
    And Navigate to SignUp Page
    And Click on Login link
    And User enter email and Password
    And User click Login Button
    Then User should get error message

  Scenario: Navigating to Login Page through Login button from NavBar
    Given User is on Landing Page
    And Click on Login button in NavBar
    And User enter email and Password
    And User click Login Button
    Then User should get error message

I created a step definition file through "create all step definition" option.
Login Step Definition:

public class LoginSteps {
    @Autowired
    private WebDriver webDriver;

    @Autowired
    private LandingPage landingPage;

    @Autowired
    private LoginPage loginPage;

    @Autowired
    private SignUpPage signUpPage;

    @Autowired
    private QuestionnairePage questionnairePage;

    @Given("User is on Landing Page")
    public void userIsOnLandingPage() {
        Assert.assertTrue(landingPage.btnGetStarted.isDisplayed());
    }

    @And("Navigate to Login Page")
    public void navigateToLoginPage() {
        webDriver.navigate().to("http://localhost:4200/login");
//        landingPage.btnGetStarted.click();
    }

    @And("User enter email and Password")
    public void userEnterEmailAndPassword() {
        loginPage.LoginDetails();
    }

    @And("User click Login Button")
    public void userClickLoginButton() {
        loginPage.btnLogininLoginPage.click();
    }

    @Then("User should be navigated to Questionnaire form")
    public void userShouldBeNavigatedToQuestionnaireForm() {
        System.out.println("User Navigated to Questionnaire page");
//        Assert.assertTrue(questionnairePage.qusTitle.getText().contentEquals("Questionnaire Form"));
    }

    @Then("User should get error message")
    public void userShouldGetErrorMessage() {
        System.out.println("Error Message");
    }

    @And("Navigate to SignUp Page")
    public void navigateToSignUpPage() {
        webDriver.navigate().to("http://localhost:4200/Signup");
//        landingPage.btnsignUpForFree.click();
    }

    @And("Click on Login link")
    public void clickOnLoginLink() {
        webDriver.navigate().to("http://localhost:4200/login");
//        signUpPage.lnkLogin.click();
    }

    @And("Click on Login button in NavBar")
    public void clickOnLoginButtonInNavBar() {
        webDriver.navigate().to("http://localhost:4200/login");
//        landingPage.btnNavBarLogin.click();
    }

}

Cucumber Hook file:

public class Hooks {
    @Autowired
    private WebDriver webDriver;

    @Value("${app.url}")
    private String appUrl;

    @Before
    public void InitializeTest(Scenario scenario){
        webDriver.navigate().to(appUrl);
        webDriver.manage().window().maximize();
    }

    @After
    public void TearDownTest(Scenario scenario){
        if(scenario.isFailed()){
            System.out.println(scenario.getName());
        }
        webDriver.quit();
    }
}

TestRunner:

@CucumberOptions(
        features = {"src/test/java/com/ksupwlt/stepcounttracker/features"},
        glue = "com.ksupwlt.stepcounttracker.steps"
)
public class TestRunner extends AbstractTestNGCucumberTests {

Without using the webdriver.quit() the browser is closed. is it necessary to add the @After? Test Report:
Login with InValid Credential 1.95 spassedBefore 16 msfailedUser is on Landing Page Step failedorg.openqa.selenium.NoSuchSessionException: Session ID is null. Using WebDriver after calling quit()? Build info: version: '4.1.2', revision: '9a5a329c5a' System info: host: 'DESKTOP-55B4PJT', ip: '192.168.1.66', os.name: 'Windows 11', os.arch: 'amd64', os.version: '10.0', java.version: '19.0.2' Driver info: org.openqa.selenium.chrome.ChromeDriver Command: [null, findElement {using=xpath, value=//button[contains(text(),'Get Started')]}] Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 111.0.5563.65, chrome: {chromedriverVersion: 110.0.5481.77 (65ed616c6e8e..., userDataDir: C:\Users\santh\AppData\Loca...}, goog:chromeOptions: {debuggerAddress: localhost:55962}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), se:cdp: ws://localhost:55962/devtoo..., se:cdpVersion: 111.0.5563.65, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:virtualAuthenticators: true} at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:145) at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:167) at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:142) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:558) at org.openqa.selenium.remote.ElementLocation$ElementFinder$2.findElement(ElementLocation.java:162) at org.openqa.selenium.remote.ElementLocation.findElement(ElementLocation.java:60) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:382) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:374) at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:70) at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:39) at jdk.proxy2/jdk.proxy2.$Proxy118.isDisplayed(Unknown Source) at com.ksupwlt.stepcounttracker.steps.LoginSteps.userIsOnLandingPage(LoginSteps.java:35) at ?.User is on Landing Page(file:///C:/Sandy/Spring_2023/Capstone/KSU%20Patient%20Weight%20Loss%20Tracker/src/test/java/com/ksupwlt/stepcounttracker/features/Login.feature:12) 0 msignoredNavigate to Login Page 0 msignoredUser enter email and Password 0 msignoredUser click Login Button 0 msignoredUser should get error message 238 mspassedAfter

vaqhlq81

vaqhlq811#

我添加了pageScope文件,并在pages文件中将@component更改为@pageScope(登录页面)。它工作正常,我还删除了webdriver。quit()。一旦测试完成,浏览器就会被selenium关闭。

import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.lang.annotation.*;

@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Documented
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface PageScope {
}

相关问题