Swipe using the Appium tool

Asked

Viewed 332 times

0

I’m learning about a new mobile automated testing tool called Appium, and one of the tests I created was:

@Test
      public void Swipe () throws InterruptedException{
        Thread.sleep(4000);
        System.out.println("Vamos fazer swipe no tutorial");
        driver.swipe(75, 0, 500, 0, 10);
       }

when running the test, it passes. However, I can’t see it doing Swipe function, I don’t understand how it manages to pass. Why does he pass?

What I wanted to get with my test: do a horizontal Swipe, right to left.

1 answer

1

If you have not entered any assert in your test, and no line of code has triggered an exception, your test will even pass (because there was no error). You should check the status of your app after running "Swipe" using some Assert or even firing an exception.

// assert com testng
Assert.assertEquals(<valor atual>, <valor esperado>);
// disparando exceção
throw new RuntimeException("Erro no app");

See an example of how to use the Swipe method (extracted from https://discuss.appium.io/t/screen-orientation-and-element-relative-swiping-tutorial/52)

public void swipeElementExample(WebElement el) {
  String orientation = driver.getOrientation().value();

  // pega a coordenada X do canto superior esquerdo do elemento, e adiciona a largura do elemento pra pegar o valor X da extrema direita do elemento
  int leftX = el.getLocation().getX();
  int rightX = leftX + el.getSize().getWidth();

  // pega a coordenada Y do canto superior esquedo do elemento e subtrai a altura pra pegar o menor valor Y do elemento
  int upperY = el.getLocation().getY();
  int lowerY = upperY - el.getSize().getHeight();
  int middleY = (upperY - lowerY) / 2;

  if (orientation.equals("portrait")) {
    // Swipe do centro-esquerdo para o centro-direito do elemento, em 500 ms
      driver.swipe(leftX + 5, middleY, rightX - 5, middleY, 500);
  }
  else if (orientation.equals("landscape")) {
    // Swipe do centro-direito para o centro-esquerdo do elemento, em 500ms
    driver.swipe(rightX - 5, middleY, leftX + 5, middleY, 500);
  }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.