【问题标题】:Selenium code opens the Chrome Browser and goes to the URL but does not type the UserID and Password and so onSelenium 代码打开 Chrome 浏览器并转到 URL 但不输入用户 ID 和密码等
【发布时间】:2020-04-21 13:02:07
【问题描述】:

我是 Selenium Webdriver 与 Cucumber 集成的新手,我无法成功登录。

这是我的功能文件:

Feature: Validate user can login successfully

@tag
    Scenario: Successful login
        Given User is on the Login screen
        When User enters UserID and Password
        And User clicks Login button
        Then Home Page is successfully displayed

这是我的步骤定义:

public class Steps

{
    WebDriver driver;

    @Given("^User is on the Login screen$")
    public void User_is_on_the_Login_screen() throws Throwable
    {
        System.out.println("Pumasok dito");
        //OPEN THE BROWSER
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\I689629\\eclipse-workspace\\uat-core\\src\\test\\resources\\drivers\\chromedriver.exe");

        WebDriver driver = new ChromeDriver();

        driver.manage().window().maximize();
        driver.get("http://go/eruat8");
        throw new PendingException();
    }

    @When("^User enters \"(.*)\" and \"(.*)\"$")
    public void User_enters_UserID_and_Password() throws Throwable
    {
            //ENTER THE FID
            driver.findElement(By.id("txtUserDefault")).sendKeys("User123");
            //driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

            //ENTER THE PASSWORD
            driver.findElement(By.id("txtPassDefault")).sendKeys("Password123");

    }

    @And("^User clicks Login button$")
    public void User_clicks_Login_button() throws Throwable
    {
        //CLICK THE LOGIN BUTTON
        driver.findElement(By.xpath("//*[@id=\"frmDefault\"]/div[1]/div[2]/div[2]/div[2]/input")).click();
        //}

    }

    @Then("^Home Page is successfully displayed$")
    public void Home_Page_is_successfully_displayed() throws Throwable
    {
        System.out.println("Home page is displayed");
    }
}

这是我的测试运行器:

    @RunWith(Cucumber.class)

    @CucumberOptions(plugin = { "pretty", "html:target/cucumber-html-report", "json:target/cucumber.json" },
    features = {"src/test/resources/features" }, glue = { "StepDefinition" })

    public class TestRunner

    {

    }

谁能帮帮我。谢谢。

【问题讨论】:

  • 请在方法外将private WebDriver driver; 声明为类变量。然后你应该能够在同一个类的其他步骤中访问驱动程序。

标签: selenium junit cucumber


【解决方案1】:
public class Steps

{

 System.setProperty("webdriver.chrome.driver", "C:\\Users\\I689629\\eclipse-workspace\\uat-core\\src\\test\\resources\\drivers\\chromedriver.exe"); 

 WebDriver driver = new ChromeDriver();

@Given("^User is on the Login screen$")
public void User_is_on_the_Login_screen() throws Throwable
{
    driver.manage().window().maximize();
    driver.get("http://go/eruat8");

}

@When("^User enters \"(.*)\" and \"(.*)\"$")
public void User_enters_UserID_and_Password() throws Throwable
{
        //ENTER THE FID
        driver.findElement(By.id("txtUserDefault")).sendKeys("User123");


        //ENTER THE PASSWORD
        driver.findElement(By.id("txtPassDefault")).sendKeys("Password123");

}

@And("^User clicks Login button$")
public void User_clicks_Login_button() throws Throwable
{
    //CLICK THE LOGIN BUTTON
    driver.findElement(By.xpath("//*[@id=\"frmDefault\"]/div[1]/div[2]/div[2]/div[2]/input")).click();

}

@Then("^Home Page is successfully displayed$")
public void Home_Page_is_successfully_displayed() throws Throwable
{
    System.out.println("Home page is displayed");
}
}

【讨论】:

    猜你喜欢
    • 2017-07-26
    • 1970-01-01
    • 2022-07-06
    • 1970-01-01
    • 1970-01-01
    • 2021-06-10
    • 2022-12-18
    • 2016-12-09
    • 1970-01-01
    相关资源
    最近更新 更多