utilizando espresso e uiautomator no teste de apps android

50
Íntel Software and Services Group Utilizando espresso e uiautomator Eduardo Carrara Developer Evangelist – Intel Developers Relations Division

Upload: eduardo-carrara-de-araujo

Post on 12-Aug-2015

246 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Utilizando Espresso e UIAutomator no Teste de Apps Android

Íntel Software and Services Group

Utilizando espresso e uiautomatorEduardo CarraraDeveloper Evangelist – Intel Developers Relations Division

Page 2: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group

#AndroidOnIntel

2

+EduardoCarraraDeAraujo

https://www.facebook.com/ducarrara

@DuCarrara

br.linkedin.com/in/eduardocarrara/

ecarrara-araujo

Page 3: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group 3

Page 4: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Information Technology

“I choose a lazy person to do a hard job. Because a lazy person will find an easy way to do it.”

- Bill Gates

4

Image by Karla Vidal @ http://www.flickr.com/photos/63721650@N00/3661526274Creative Commons cc-by-2.0

Page 5: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group

Automação de Testes

Testes de Integração

Fragmentação e Testes

5

Problemas?

Page 6: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group 6

Acceptance Testing

System Testing

Integration Testing

Unit Testing JUnit

JUnit

JUnit UI Automator

Page 7: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group 7

JUnit

Page 8: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services GroupIntel Information Technology

Android Testing Framework

8

• Special TestCases, Asserts e Mocks

• MoreAsserts

• ViewAsserts

• MockApplication

• MockContext

• MockContentProvider

• AndroidTestCase

• ActivityInstrumentationTestCase2

• ActivityUnitTestCase

• ApplicationTestCase

• InstrumentationTestCase

• ProviderTestCase

• ServiceTestCase

• SingleLaunchActivityTestCaseJUnit

Page 9: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group

Instrumentation

9

• Android Components Context Access;

• Components Lifecycle Control;

• Component Loading Control;

• System Events (e.g.: Broadcasts)

• InstrumentationTestRunner

• AndroidJUnitRunner

• GoogleInstrumentationTestRunner

Page 10: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group

Espresso

10

• Simplifies the UI Test Process within your App

• Methods for:

• View matching

• Checks

• UI Events

Page 11: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group 11

Espresso - Configuração

dependencies {androidTestCompile 'com.android.support.test:runner:0.3'androidTestCompile 'com.android.support.test:rules:0.3'androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'

}

build.gradle

Desabilitar nas opções do desenvolvedor (no device):• Window Animation Scale• Transition Animation Scale• Animator Duration Scale

Page 12: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group

1. Crie uma subclasse de ActivityInstrumentationTestCase2

2. Identique o(s) componente(s) de UI que você quer testar.

3. Simule a interação do usuário com o componente.

4. Repita os passos utilizando assertions para verificar se a UI reflete o estado esperado depois das interações.

12

Espresso – Primeiro Test Case

Page 13: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group 13

Espresso – Caso Vilibra

• Bibliotecário Virtual

• Ajudar a lembrar os livros emprestados.

• Nesta caso como automatizar o teste da visualização de detalhes de um empréstimo?

• Classe: TestViewLendedBookDetailFlow

Page 14: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group 14

Espresso – Caso Vilibra

public class TestViewLendedBookDetailFlow

extends ActivityInstrumentationTestCase2<BookListActivity> {

}

public TestViewLendedBookDetailFlow() {

super(BookListActivity.class);

}

...

...

Page 15: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group 15

Espresso – Caso Vilibra

public class TestViewLendedBookDetailFlow

extends ActivityInstrumentationTestCase2<BookListActivity> {

}

...

...

@Before

public void setUp() throws Exception {

super.setUp();

injectInstrumentation(InstrumentationRegistry.getInstrumentation());

mBookListActivity = getActivity();

prepareTestData();

}

Page 16: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group 16

Espresso – Caso Vilibra

public class TestViewLendedBookDetailFlow

extends ActivityInstrumentationTestCase2<BookListActivity> {

}

...

...

@After

public void tearDown() throws Exception {

clearTestData();

}

Page 17: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group 17

Espresso – Caso Vilibra

public class TestViewLendedBookDetailFlow

extends ActivityInstrumentationTestCase2<BookListActivity> {

}

...

...

@Test

public void testViewLendedBookDetailFlow() {

}

onData(CursorMatchers.withRowString(

VilibraContract.BookEntry.COLUMN_TITLE,

mBookTitle))

.inAdapterView(withId(R.id.lended_book_list_view))

.perform(click());

continua

Page 18: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group 18

Espresso – Caso Vilibra

public class TestViewLendedBookDetailFlow

extends ActivityInstrumentationTestCase2<BookListActivity> {

}

...

...

@Test

public void testViewLendedBookDetailFlow() {

}

// check data in the detail screen

onView(withId(R.id.book_title_text_view))

.check(matches(withText(mBookTitle)));

onView(withId(R.id.book_subtitle_text_view))

.check(matches(withText(mBookSubtitle)));

continua

Page 19: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group 19

Espresso – Caso Vilibra

public class TestViewLendedBookDetailFlow

extends ActivityInstrumentationTestCase2<BookListActivity> {

}

...

...

@Test

public void testViewLendedBookDetailFlow() {

}

onView(withId(R.id.book_authors_text_view))

.check(matches(withText(mBookAuthors)));

onView(withId(R.id.book_publisher_edition_text_view))

.check(matches(withText(mBookPublisher)));

continua

Page 20: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group 20

Espresso – Caso Vilibra

public class TestViewLendedBookDetailFlow

extends ActivityInstrumentationTestCase2<BookListActivity> {

}

...

...

@Test

public void testViewLendedBookDetailFlow() {

}

pressBack();

Fim

Page 21: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group

UI Automator

21

• Testes Cross App

• Baseado em Instrumentação

• Interage com elementos visíveis utilizando descritores convenientes (como texto).

Page 22: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group 22

UIAutomator - Configuração

build.gradle

É necessário inspecionar os componentes visuais das apps alvo para garantir que o UIAutomator consiga localizá-los.Isso significa ter labels de texto visíveis, a propriedade android:contentDescriptionpreenchida, ou ainda a propriedade android:hint (para EditTexts)

dependencies {androidTestCompile 'com.android.support.test:runner:0.3'androidTestCompile 'com.android.support.test:rules:0.3'androidTestCompile 'com.android.support.test.uiautomator:uiautomator-

v18:2.1.1'}

Page 23: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group

1. Crie uma subclasse de InstrumentationTestCase

2. Obtenha uma instância de UIDevice.

3. Utilize o método UIDevice.findObject() para obter UIObjects e executar ações.

4. Utilize asserts para verificar os resultados.

23

UIAutomator – Primeiro Test Case

Page 24: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group 24

UIAutomator - uiautomatorviewer

Page 25: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group 25

UIAutomator – Caso Vilibra

• Novo caso: automatizando o registro de um empréstimo.

• Classe: TestLendBookFlow

Page 26: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group 26

UIAutomator – Caso Vilibrapublic class TestLendBookFlow extends InstrumentationTestCase {

}

...

...

@Before

public void setUp() throws UiObjectNotFoundException {

}

clearTestData();

mUiDevice = UiDevice.getInstance(getInstrumentation());

// Should start from the home screen

mUiDevice.pressHome();

continua

Page 27: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group 27

UIAutomator – Caso Vilibrapublic class TestLendBookFlow extends InstrumentationTestCase {

}

...

...

@Before

public void setUp() throws UiObjectNotFoundException {

}

continua

UiObject allAppsButton = mUiDevice.findObject(new UiSelector()

.description("Apps"));

assertTrue(allAppsButton.exists());

allAppsButton.clickAndWaitForNewWindow();

Page 28: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group 28

UIAutomator – Caso Vilibrapublic class TestLendBookFlow extends InstrumentationTestCase {

}

...

...

@Before

public void setUp() throws UiObjectNotFoundException {

}

continua

UiObject appsTab = mUiDevice.findObject(new UiSelector()

.text("Apps"));

assertTrue(appsTab.exists());

appsTab.click();

Page 29: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group 29

UIAutomator – Caso Vilibrapublic class TestLendBookFlow extends InstrumentationTestCase {

}

...

...

@Before

public void setUp() throws UiObjectNotFoundException {

}

continua

// find the scrollable list of apps

UiScrollable appsList = new UiScrollable(new UiSelector()

.scrollable(true));

appsList.setAsHorizontalList();

Page 30: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group 30

UIAutomator – Caso Vilibrapublic class TestLendBookFlow extends InstrumentationTestCase {

}

...

...

@Before

public void setUp() throws UiObjectNotFoundException {

}

Fim! Do setup...

UiObject vilibraApp = appsList.getChildByText(new UiSelector()

.className("android.widget.TextView"), "ViLibra");

vilibraApp.click();

mUiDevice.wait(Until.hasObject(By.pkg("ecarrara.eng.vilibra")),

5000L);

Page 31: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group 31

UIAutomator – Caso Vilibrapublic class TestLendBookFlow extends InstrumentationTestCase {

}

...

...

}

@Test

public void testLendBookFlow() throws UiObjectNotFoundException {

UiObject lendBookButton = mUiDevice.findObject(new UiSelector()

.resourceId("ecarrara.eng.vilibra:id/add_lending_action_button")

.className("android.widget.ImageButton"));

lendBookButton.clickAndWaitForNewWindow();

continua

Page 32: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group 32

UIAutomator – Caso Vilibrapublic class TestLendBookFlow extends InstrumentationTestCase {

}

...

...

}

@Test

public void testLendBookFlow() throws UiObjectNotFoundException {

UiObject isbnEditText = mUiDevice.findObject(new UiSelector()

.text("ISBN")

.className("android.widget.EditText"));

isbnEditText.setText(BOOK_ISBN);

continua

Page 33: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group 33

UIAutomator – Caso Vilibrapublic class TestLendBookFlow extends InstrumentationTestCase {

}

...

...

}

@Test

public void testLendBookFlow() throws UiObjectNotFoundException {

continua

UiObject confirmButton = mUiDevice.findObject(new UiSelector()

.text("Confirm")

.className("android.widget.Button"));

confirmButton.clickAndWaitForNewWindow();

Page 34: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group 34

UIAutomator – Caso Vilibrapublic class TestLendBookFlow extends InstrumentationTestCase {

}

...

...

}

@Test

public void testLendBookFlow() throws UiObjectNotFoundException {

continua

UiObject isbnTextView = mUiDevice.findObject(new UiSelector()

.className("android.widget.TextView")

.resourceId("ecarrara.eng.vilibra:id/book_isbn10_text_view"));

Assert.assertTrue(isbnTextView.getText().contains(BOOK_ISBN));

Page 35: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group 35

UIAutomator – Caso Vilibrapublic class TestLendBookFlow extends InstrumentationTestCase {

}

...

...

}

@Test

public void testLendBookFlow() throws UiObjectNotFoundException {

continua

UiObject lendButton = mUiDevice.findObject(new UiSelector()

.className("android.widget.Button")

.text("Lend this Book"));

lendButton.clickAndWaitForNewWindow();

Page 36: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group 36

UIAutomator – Caso Vilibrapublic class TestLendBookFlow extends InstrumentationTestCase {

}

...

...

}

@Test

public void testLendBookFlow() throws UiObjectNotFoundException {

continua

UiObject contactView = mUiDevice.findObject(new UiSelector()

.className("android.widget.TextView")

.text("Meu Irmao"));

contactView.click();

mUiDevice.wait(Until.hasObject(By.pkg("ecarrara.eng.vilibra")),

500L);

Page 37: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group 37

UIAutomator – Caso Vilibrapublic class TestLendBookFlow extends InstrumentationTestCase {

}

...

...

}

@Test

public void testLendBookFlow() throws UiObjectNotFoundException {

Fim

UiObject bookTitleTextView = mUiDevice.findObject(new UiSelector()

.className("android.widget.TextView")

.resourceId("ecarrara.eng.vilibra:id/book_name_text_view")

.text(BOOK_TITLE));

Assert.assertTrue(bookTitleTextView.exists());

Page 38: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group 38

“If you don’t like testing your product, most likely your customers won’t like

to test it either.” - Anonymous

Page 39: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group 39

Fragmentação

Variedade de tamanhos de tela

Arquiteturas Diferentes

Versões diferentes de OS

Page 40: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group 40

Page 41: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group 41

Page 42: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group 42

Page 43: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group 43

Page 44: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group 44

Page 45: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Information Technology

What is next?

45

• Experimente automatizar os testes de sua app!

• Como fazer Integração e Entrega Contínua com Android?

• Cobertura de Código

• Mocking

Page 46: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group 46

Intel Developer Zone

https://software.intel.com/en-us/android/app-testing

Page 47: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group 47

Nos Avalie!

http://bit.ly/IntelPesquisa

Page 48: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group

Thanks!

48

+EduardoCarraraDeAraujo

https://www.facebook.com/ducarrara

@DuCarrara

br.linkedin.com/in/eduardocarrara/

ecarrara-araujo/vilibra

Page 49: Utilizando Espresso e UIAutomator no Teste de Apps Android

Intel Software and Services Group 49

References

• Android Testing: https://developer.android.com/tools/testing/testing_android.html

• Android Unit Testing Support: http://tools.android.com/tech-docs/unit-testing-support

• UI Testing: https://developer.android.com/training/testing/ui-testing/index.html

• Android Testing Support Library: https://developer.android.com/tools/testing-support-library

• Android Instrumentation: http://developer.android.com/tools/testing/testing_android.html#Instrumentation

• Junit: http://junit.org

• Testdroid: http://testdroid.com

• Intel App Testing Page: https://software.intel.com/en-us/android/app-testing

Page 50: Utilizando Espresso e UIAutomator no Teste de Apps Android

Placeholder Footer Copy / BU Logo or Name Goes Here