首页 > 解决方案 > 宁静剧本模式-上传文件

问题描述

我们正在尝试 Cucumber Serenity 框架进行端到端测试。我是相当新的技术,我厌倦了下面这个简单的代码。

actor.attemptsTo(Enter.theValue(path).into(Upload));

其中路径是我尝试使用浏览器的上传小部件上传的文件的位置。有没有人设法使用宁静屏幕播放模式执行这样的操作。

它真的让我们想到放弃宁静而只使用黄瓜硒框架,因为我可以使用 Upload.sendkeys(path); 轻松执行此操作;任何帮助深表感谢。提前致谢。

在此处输入图像描述

在此处输入图像描述

按要求:上市步骤:

public class ListingSteps
{
  @Before
  public void set_the_stage() {
    OnStage.setTheStage(new OnlineCast());
  }

  @Given("^(.*) is able to click import products$") public void userIsAbleToClick(String actorName) throws Throwable
  {
    theActorCalled(actorName).wasAbleTo(Start.theApplication());

  }
  @When("^s?he imports a  single item successfully$") public void heImportsASingleItemSuccessfully() throws Throwable
  {
    theActorInTheSpotlight().attemptsTo(Import.spreadsheet());
  }
  @Then("^(.*) are listed on ebay and amazon with all the right information$") public void itemsAreListedOnEbayAndAmazonWithAllTheRightInformation(String actorName, String SKU)
     throws Throwable

{ //待办的

  }

暂时忽略它,因为它正在进行中。

导入类:

public class Import implements Task
{
  protected String path =
      "C:\\somePathToFile\\populated_excel.xlsx";
  public static Import spreadsheet()
  {
    return instrumented(Import.class);
  }
  @Override public <T extends Actor> void performAs(T actorName)
  {
    actorName.attemptsTo(Click.on(Products.ProductsScreen));
    actorName.attemptsTo(Click.on(Products.Upload));
    actorName.attemptsTo(Enter.theValue(path).into(Browse).thenHit(Keys.RETURN));//this is the line which is giving errors
    actorName.attemptsTo(Click.on(Products.UploadButton));
  }
}

目标浏览

public class Products
{
 public static Target Browse = Target.the("browse file").locatedBy("//input[@type='file']");
}

标签: javaserenity-bdd

解决方案


我设法让这个工作的方式是使用 FileToUpload 类:

import net.thucydides.core.pages.components.FileToUpload;

FileToUpload fileToUpload = new FileToUpload(driver, fileName);    
fileToUpload.fromLocalMachine().to(webElement);

推荐阅读