当前位置: 首页 > 知识库问答 >
问题:

打开浏览器

晋功
2023-03-14
 Feature: Login to Website

  #Login in Chrome
  Scenario: Login using Chrome
  Given I open Chrome
  When I browse to Website
  Then I login to Website using "user1" and "password1"

全局基类

public class base {

public WebDriver driver;
public Properties prop;
public ChromeOptions coptions;

public WebDriver initializeDriver() throws IOException
{

    String browserName= "chrome";
    System.out.println(browserName);
    String pathToDriver = "";

    if(browserName.equals("chrome"))
    {
        pathToDriver = "C:\\Repositories\\automationProject\\webDrivers\\chromedriver.exe";
        System.setProperty("webdriver.chrome.driver", pathToDriver);
        coptions.addArguments("disable-infobars");
        coptions.addArguments("--start-maximized");
        driver= new ChromeDriver(coptions);
        //execute in chrome driver

    }

    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    return driver;
}

登录名

public class login extends base {

@Given("^I open Chrome$")
public void iOpenChrome() throws Throwable {
    driver = initializeDriver();
}

@When("^I browse to Website$")
public void iBrowseToWebsite() {
    driver.get("https://www.website.com/");
}

@Then("^I login to Website using \"([^\"]*)\" and \"([^\"]*)\"£\"$")
public void iLoginToWebsiteUsingAnd£(String username, String password) throws Throwable {
    driver.findElement(By.id("UserName")).sendKeys(username);
    driver.findElement(By.id("Password")).sendKeys(password);
    driver.findElement(By.id("btnLogin")).click();
}

}

问题是,当运行此功能时,我得到了下面的错误,我不明白为什么会发生这种情况,因为它在错误的位置上没有给我任何帮助。

未定义步骤:给定I打开Chrome

未定义步骤:浏览网站时

未定义的步骤:然后我使用“user1”和“password1”登录网站

共有3个答案

徐峰
2023-03-14

您应该通过右键单击将cucumber功能文件转换为StepDefention文件不要键入代码行

姬浩渺
2023-03-14

您的步骤的函数名称是错误的。您遵循的是camelCase方法,在cucumber的情况下无效。您已将您的@Given("^I openChrome$")步骤函数名称写入i_open_chrome()而不是iOpenChrome()。您可以使用Tidy Gherkinchrome扩展来生成步骤定义。

有效的示例步骤。

@Given("^I open Chrome$")
public void i_open_chrome() throws Throwable {
    throw new PendingException();
}

@When("^I browse to Website$")
public void i_browse_to_website() throws Throwable {
    throw new PendingException();
}

@Then("^I login to Website using \"([^\"]*)\" and \"([^\"]*)\"$")
public void i_login_to_website_using_something_and_something(String strArg1, String strArg2) throws Throwable {
    throw new PendingException();
}
陆洛城
2023-03-14

可能您的“胶水”没有与您的测试运行程序或配置正确对齐。

尝试在“Run”中编辑“glue”选项

或者,如果您使用的是test runner类,请确保您得到以下信息:

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(
        features = "path\\to\\features",
        glue = "stepdefs"
)
public class RunTest {

}

其中,stepdefs是包含步骤定义文件的包名。

 类似资料:
  • 我有以下场景:安装了firefox和chrome的Ubuntu16.04机器,chrome是我的默认浏览器。 我在shell中检查以下操作: 并且不会出现firefox的任何一面,总是Chrome。这样很好。还要检查: 点击桌面文件。html->chrome启动 现在奇怪的是,如果我跑了: 出于某种奇怪的原因,它总是打开Firefox。我不知道为什么,有人能帮我吗?

  • 我在Firefox中远程使用Webdriver。 我想全屏打开我的浏览器。浏览器是全屏打开的,但立即最小化,并移动到其他程序,在我的操作系统上打开。当我在本地运行我的webdriver时,broser是全屏打开的,并且不会最小化(它留在浏览器中,不会移动到其他程序)。我希望我的浏览器会在全屏打开,并且保持在浏览器中,即使我运行我的测试Remottley。 原因是我使用了Java机器人,我必须在浏览

  • 本文向大家介绍在Nginx浏览器中打开目录浏览功能,包括了在Nginx浏览器中打开目录浏览功能的使用技巧和注意事项,需要的朋友参考一下 在nginx中不像apache默认是打开目录浏览功能的,在nignx中目录浏览功能默认是关闭了,下面我来介绍在nginx中实现目录浏览功能的配置方法。 打开nginx.conf文件,在location server 或 http段中加入 另外两个参数最好也加上去:

  • 问题内容: 出于营销原因,我正在使用selenium打开一些浏览器窗口。我只是打开我的营销渠道,通过selenium登录并开始工作。 问题是,在执行代码后,selenium将关闭窗口。 到目前为止,所有解决方案均无济于事。 我有13个浏览器窗口atm。,如下所示: 我找到的最接近的解决方案是在脚本的末尾添加此内容,但是不知何故,它只能保持5个窗口打开,而不是关闭5个窗口并打开下5个新窗口: 我只希

  • 问题内容: 我想执行XMLHttpRequest,然后通过POST方法发送文件名在浏览器中打开PDF。 这可能吗,或者XMLHttpRequest仅用于HTML? 问题答案: 如果您查询的URL实际上返回PDF数据,则无法通过XMLHttpRequest进行操作。 为什么?因为该响应是包含原始PDF数据的HTTP响应。即使您确实可以通过responseText`属性访问数据,JavaScript也

  • 有可能让selenium使用TOR浏览器吗?有人有什么代码可以复制粘贴吗?