【问题标题】:AWS Device Farm - extra data path in scriptAWS Device Farm - 脚本中的额外数据路径
【发布时间】:2018-09-27 17:31:10
【问题描述】:

我的 Appium 脚本在本地完美运行,但移至 aws 设备场,由于一个类文件而返回解析错误。 我正在尝试从此类文件中的 excel 文件中导入数据。我认为是因为excel文件的路径出错。

我将数据 excel 文件作为额外数据上传到 aws,但我找不到位置。

public static void changeCity() throws InterruptedException{
try{
    File src = new File("data1.xls");
    Workbook wb = Workbook.getWorkbook(src);
    Sheet sh1 = wb.getSheet(0);
    Sheet bugzillaUpdation = wb.getSheet("UtilityCredentials"); 

请帮我解决问题。


@jmp

我使用了 junit 并将位置设置为 File src = new File("/acme-android-appium/src/test/resources/data1.xls");

我不清楚你上面说的那个 XML 文件。你能解释一下我们如何在我的脚本中找到文件。

请看附件图片。

【问题讨论】:

    标签: appium


    【解决方案1】:

    我在 AWS Device Farm 团队工作。

    要读取.xlsx.csv 文件,可以使用以下两种方法:

    1. 确保excel文件放在src/test/resources/com/yourexcelfile.xlsx/csv
    2. 这会将文件放在com 文件夹下的测试jar 文件中。
    3. 一旦确认,您应该能够使用以下两个代码 sn-ps 之一读取文件:

    没有任何外部库:

    java.net.URL resource = ClassLoader.getSystemResource("/com/yourexcelfile.xlsx");
    File file = new File(resource.toURI());
    FileInputStream input = new FileInputStream(file);
    

    如果您使用 Apache POI API 来读取 excel 文件:

    InputStream ins = getClass().getResourceAsStream(“/com/yourexcelfile.xlsx”);
    workbook = new XSSFWorkbook(ins);
    sheet = workbook.getSheetAt(1);
    ins.close();
    

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      如果您尝试将文件放入项目的 src/test/java/resources/ 会发生什么?这样,它将使用 jar 构建,然后设备场可能会引用它。

      [更新]

      我自己使用 awslabs github 页面[1] 中的示例项目进行了尝试。我还可选地创建了一个包含此 xml 的 testng.xml 文件:

      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
      <suite name="Default Suite">
          <test name="test">
              <classes>
                  <class name="file.FindFile"/>
              </classes>
          </test>
      </suite>
      

      此文件位于 /Referenceapp-Appium-Test/src/test/resources/testng.xml 并使用此插件从 pom.xml 中引用:

      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.12.4</version>
          <configuration>
          <suiteXmlFiles>
              <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
          </suiteXmlFiles>
          </configuration>
      </plugin>
      

      testng.xml 在这里仅用于运行特定测试,而不是示例项目中的所有测试。它是可选的,仅供参考。

      然后我在同一目录中创建了一个 test.csv 文件并创建了一个新的包“文件”,其中包含一个测试类“FindFile”:

      package file;
      
      import java.io.File;
      
      import org.testng.annotations.Test;
      
      import Pages.BasePage;
      import io.appium.java_client.AppiumDriver;
      
      public class FindFile extends BasePage {
           protected FindFile(AppiumDriver driver) {
              super(driver);
              // TODO Auto-generated constructor stub
          }
      
           @Test
          public static void changeCity() throws InterruptedException{
                  try{
                      File src = new File("/Referenceapp-Appium-Test/src/test/resources/test.csv");
                      System.out.println("File found!!!");
                  }catch(Exception e){
                      System.out.println(e);
                  }
              }
      }
      

      所以当我在设备场中使用随机 apk 执行它时,我让它只执行 FindFile.java 内部的测试。当我查看 appium java 输出时,我在那里看到了我的 println,所以我知道它是如何工作的。

      [TestNG] RUNNING: Suite: "Command line test" containing "1" Tests (config: null)
      [TestNG] INVOKING: "Command line test" - file.FindFile.changeCity()
      [Invoker 1121172875] Invoking file.FindFile.changeCity
      File found!!!
      [TestNG] PASSED: "Command line test" - file.FindFile.changeCity() finished in 35 ms
      ===== Invoked methods
          FindFile.changeCity()[pri:0, instance:null]  <static>
      =====
      Creating /tmp/scratchLT6UDz.scratch/resultsm_f_bN/Command line suite/Command line test.html
      Creating /tmp/scratchLT6UDz.scratch/resultsm_f_bN/Command line suite/Command line test.xml
      PASSED: changeCity
      

      希望有帮助

      最好的问候

      詹姆斯

      [1]https://github.com/awslabs/aws-device-farm-appium-tests-for-sample-app

      【讨论】:

      • 文件 src = new File("/Referenceapp-Appium-Test/src/test/resources/test.csv");这对我不起作用。文件路径仍然错误。
      • 愚蠢的问题,但您在该目录中添加了一个 test.csv 对吗?
      • 您能否提供更多关于您如何遵循我所做的事情的信息?
      • 请查看包含屏幕截图的帖子。我发表评论作为答案。
      • 我还没有测试过这个想法,但是如果这是一个 maven 项目并且测试被压缩并上传,为什么不将额外的文件包含在压缩的测试包中呢?编写添加它的 maven 插件应该相当容易。在编写之前,只需尝试添加文件并在 Device Farm 中访问它。
      猜你喜欢
      • 1970-01-01
      • 2017-06-26
      • 1970-01-01
      • 2019-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-03
      相关资源
      最近更新 更多