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

如何用selenium和cucumber启动浏览器

韩明德
2023-03-14

有人能帮我吗?

提前谢了。

共有1个答案

羊舌富
2023-03-14

您可以在Cucumber主站点Cucumber文档中找到许多关于项目应该使用哪些dependecies的信息

cucumber底是红宝石

Cucumber-JVM是Java

    null
Feature: Calculator should work accourding to standard calculator devices

  Scenario: addition
    Given a calculator I just turned on
    When I add 4 and 5
    Then the result is 9
    null
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(
        monochrome = false,
        plugin = {"pretty","json:target/cucumber.json"} ,
        features = "src/test/resources/cucumber",
        tags = "~@ignore"
        )
public class RunCukesTest {
}   
  • 您必须启动WebDriver_Driver;
  • 使用driver创建新的ChromeDriver或FireFoxDriver等
  • 有些浏览器需要安装ChromeDriver firefox内置(据我所知)
  • 请参阅下面的代码
  • 问有没有什么不明白的
import java.util.concurrent.TimeUnit;

import cucumber.api.java.en.*;

import org.eclipse.jetty.util.thread.Timeout;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class NavigationSteps {

    WebDriver _driver;

    @Given("^i am at \"([^\"]*)\"$")
    public void i_am_at_home(String arg1) throws Throwable {
        System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
        _driver = new ChromeDriver();

        _driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        _driver.get(arg1);
        Thread.sleep(500);
    }

    @When("^i click on \"([^\"]*)\"$")
    public void i_click_on(String arg1) throws Throwable {
        _driver.findElement(By.linkText(arg1)).click();
        Thread.sleep(500);
    }

    @Then("^i expect the title to be \"(.*?)\"\"(.*?)\"$")
    public void i_expect_the_title_to_be(String arg1, String arg2)  throws Throwable {
        String result = (arg1 + " | " + arg2); 

        Thread.sleep(200);
        assertEquals("Title should be",result,_driver.getTitle());
        tearDown();

    }

    @Then("^Header should contain \"(.*?)\"$")
    public void header_should_contain(String arg1) throws Throwable {
        Thread.sleep(200);
        assertEquals("Title should be", arg1, _driver.findElement(By.xpath(".//*[@id='main']/div[1]/div/h1")).getText());
        tearDown();
    }

    @After
    public void tearDown() throws InterruptedException
    {

        _driver.quit();
    }

}

编辑-以更好的格式回答问题

  • feature file调用java文件(通过RunCukesTest或feature file运行)
    • 要测试的方法进入je java步骤文件
    • feature calculator有一个calculatorsteps.java文件,场景是在feature文件中步骤中的方法
    Given a calculator I just turned on
            results into
            @Given ("^a calculator I just turned on$")
            public void iCanCallThisAnythingIWant(){
            #do something
            }
    

 类似资料:
  • 我突然遇到了硒和铬驱动的错误问题。我还没有改变一件事,但我遇到了这些错误信息。这个脚本在几个小时前就开始工作了,现在没有任何调整,它不工作了。 这是我对应的代码: 更奇怪的是,当打开新终端、加载python并从selenium import webdriver中键入时,我没有收到任何错误。但是,当我导航到脚本所在的文件夹,并从selenium import webdriver加载python和类型

  • 我正在尝试启动selenium web浏览器,我可以打开火狐浏览器,但无法打开谷歌帮助我解决我面临的这个错误 线程“main”org.openqa.selenium.WebDriverException中的hello world异常:等待Firefox启动45秒时超时。构建信息:版本:“3.14.0”,修订版:“AACCCCE0”,时间:“2018-08-02T20:05:20.749Z”系统信息

  • 我对硒很陌生。我开发了一个应用程序,使用一个selenium web驱动程序来做网页上的一些动作。当我在本地运行时,它可以很好地工作,也就是说,它在我的机器中启动了一个浏览器。我在VM服务器上部署了这个应用程序,所以脚本在服务器上运行(在VM Ware机器中启动的浏览器),而不是在客户端。谁能帮助我如何在客户端启动浏览器?

  • 我试图使用eclipse来运行Selenium Web驱动程序自动化,但无法打开浏览器窗口。调试日志显示没有错误,我成功地检索了网页的标题,如下所示: 调试日志:1496221115902 geckodriver信息监听127.0.0.1:39119 1496221116097 geckodriver::木偶信息启动浏览器/应用程序/火狐.app/contents/macos/firefox-bi

  • 我的代码: 例外情况 线程“main”java中出现异常。lang.IllegalStateException:驱动程序可执行文件不存在:C:\Users\sony\Downloads\chromedriver\U win32。在com.exe。谷歌。常见的基础先决条件。org上的checkState(premissions.java:534)。openqa。硒。遥远的服务司机服务。在org上检查

  • 使用此代码为相同,但它是给无法访问的浏览器异常和不工作。