在黄瓜特征文件中传递的参数,在步骤定义脚本中不起作用

时间:2019-12-10 07:06:26

标签: selenium-webdriver cucumber bdd

查询:在黄瓜特征文件中传递的参数,在步骤定义脚本中不起作用。我试图从黄瓜传递浏览器名称,并且脚本应按输入运行。 以下是预期有效的功能文件和步骤定义

    Feature: List of scenarios.

      Scenario Outline: Add a Bank in application
        Given Open "browser" and enter a URL
        When Login to application and navigate to bank master
        Then Click on create bank and enter "Code" and "Short name" and "Description" save details
        Then Logout and Approve the Bank from checker
        Then Verify the bank is added successfully
        Then Logout the application and close the browser

    Examples: 
          | browser | Code  | Short name | Description     |
          | chrome  | Bank2 | Bank Two   | Bank Two Desc   |
          | Firefox | Bank3 | Bank Three | Bank Three Desc |

    **Step Defination is:**

    @Given("^Open (.*). and enter a URL$")
        public void open_and_enter_a_URL(String browser) throws Throwable {
            // 'firefox'
            if (browser.equalsIgnoreCase("firefox")) {
                System.setProperty("webdriver.firefox.marionette", ProjectPath+"/Drivers/geckodriver.exe");
                driver = new FirefoxDriver();
            }
            // 'chrome'
            else if (browser.equalsIgnoreCase("chrome")) {
                System.setProperty("webdriver.chrome.driver", ProjectPath+"/Drivers/chromedriver.exe"); 
                driver = new ChromeDriver();
            } // 'Edge'
            else if (browser.equalsIgnoreCase("IE")) { // set path to Edge.exe
                System.setProperty("webdriver.edge.driver", ProjectPath+"/Drivers/MicrosoftWebDriver.exe");
            } else {
                // If no browser passed throw exception
                throw new Exception(**"Browser is not correct");**
                 //Giving this error for incorrect browser as parameter is not identified in step defination 
            }
            driver.manage().window().maximize();
            driver.get(URL);
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        }

 **Getting error as:**
    java.lang.Exception: Browser is not correct
        at.stepDefination.addbankSteps.open_and_enter_a_URL(addbankSteps.java:42)
        at ✽.Given Open chrome and enter a URL(Features/TC_001.feature:4)

有人可以帮我吗?

2 个答案:

答案 0 :(得分:0)

在功能文件中,您需要提供浏览器名称,例如chrome,firefox,IE,而不是文本“浏览器”

功能:方案列表。

  Scenario Outline: Add a Baak in Liquidice
    Given Open firefox and enter a URL

答案 1 :(得分:0)

如果在步骤定义中使用(。*)正则表达式,则功能文件中不需要

双引号。请确保使用Cucumber 4.x.x或更高版本,并更新插件和IDE。

功能文件:

Feature: List of scenarios.

  Scenario Outline: Add a Bank in application
    Given Open browser and enter a URL

步骤定义:

@Given("Open (.*) and enter a URL")