【问题标题】:How do we add selectors/ids to Flutter widgets so they can be accessed from Appium我们如何将选择器/id 添加到 Flutter 小部件,以便可以从 Appium 访问它们
【发布时间】:2019-08-25 05:45:34
【问题描述】:

我们想使用 Appium/Selenium 对 Flutter 应用程序进行自动化测试。在 Selenium 中查看时,某些元素没有选择器。在 Android 中,我们只需在每个元素上添加 id,它们就会出现在 Appium 中。我们如何在 Flutter 环境中做到这一点?

【问题讨论】:

    标签: flutter appium


    【解决方案1】:

    在今天早上之前,我对 Flutter 一无所知。几个小时后,我可以放心地说“你没有”。虽然 Flutter 让应用程序的开发变得快速而轻松,但它消除了您拥有的很多控制权,包括您正在寻找的自定义级别。

    一两年前,Flutter 官方留言板上就有关于此的点击,但没有答案。

    您可以尝试通过文本定位所有内容吗? Kluge,很难或不可能维护,但目前可能是您唯一的选择。

    【讨论】:

      【解决方案2】:

      我找到了一种解决方法,它可以让您在 Flutter Web 中合理自然地使用 Selenium(尽管不适用于无头浏览器)

      1. 您需要找到窗口 x y 坐标与屏幕 x y 坐标的偏移量。我在另一个线程中发现了这个想法 pageCallibrator.html:
      <script>
      window.coordinates = [];
      document.addEventListener('click', function() {
           window.coordinates = [event.pageX, event.pageY]; 
      });
      </script>
      

      然后在 Selenium setup 运行测试之前(Java 示例)

          int windowScreenOffsetX = 0;
          int windowScreenOffsetY = 0;
      
          void callibrateXY(WebDriver driver) {
              driver.get("http://localhost:8080/pageCallibrator.html"); //TODO adjust host
              Dimension size = driver.manage().window().getSize();
      
              int x = size.width / 2;
              int y = size.height / 2;
              clickMouseAtXY(x, y);
              try {
                  Thread.sleep(500);
              } catch (InterruptedException e) {
              }
              List<Object> coordinates = (List<Object>) ((JavascriptExecutor) driver).executeScript("return window.coordinates;");
              windowScreenOffsetX = x - (int) (long) coordinates.get(0);
              windowScreenOffsetY = y - (int) (long) coordinates.get(1);
          }
      

      现在在 Selenium 中按下 Flutter 按钮

                  WebElement continueToBankButtonElement = findElementWithText(driver, "My button text");
                  clickMouseAtElement(continueToBankButtonElement);
      

      你定义的地方

      import org.openqa.selenium.*
      
          Robot robot = new Robot();
          Driver driver = new ChromeDriver(options); // TODO handler exceptions and options in a method
      
      
          WebElement findElementWithText(WebDriver driver, String text) {
              return driver.findElement(containsTextLocator(text));
          }
      
          By containsTextLocator(String text) {
              return By.xpath("//*[contains(text(), '" + text + "')]");
          }
      
          void clickMouseAtElement(WebElement element) {
              clickMouseAtXY(element.getLocation().getX() + element.getSize().width / 2, element.getLocation().getY() + element.getSize().height / 2);
          }
      
          void clickMouseAtXY(int x, int y) {
              moveMouse(x, y);
              robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
              robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
          }
      
          /**
           * @param x
           * @param y
           */
          protected void moveMouse(int x, int y) {
              robot.mouseMove(x + windowScreenOffsetX, y + windowScreenOffsetY); // Offset of page from screen
          }
      
      

      【讨论】:

      • 可能对某些场景有用,但我会远离这种方法。当您更改任何布局样式时,它会完全折叠您的测试。
      猜你喜欢
      • 2014-06-10
      • 1970-01-01
      • 1970-01-01
      • 2019-06-20
      • 1970-01-01
      • 2017-04-06
      • 2015-03-11
      • 2014-02-13
      相关资源
      最近更新 更多