Skip to content

Commit 017ae41

Browse files
committed
-Espresso Web Basic Sample
-Restructured samples to a more sane structure based on test types -Bug fixes Change-Id: If3b8acedf0dbe4fde4187cb21509c71aba727c16
1 parent 04ef00d commit 017ae41

File tree

364 files changed

+906
-68
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

364 files changed

+906
-68
lines changed

espresso/BasicSampleBundled/.classpath

-10
This file was deleted.

espresso/BasicSampleBundled/.project

-33
This file was deleted.

espresso/BasicSampleBundled/project.properties

-14
This file was deleted.

settings.gradle

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
include ':espresso:BasicSample:app'
2-
include ':espresso:CustomMatcherSample:app'
3-
include ':espresso:MultiWindowSample:app'
4-
include ':espresso:DataAdapterSample:app'
5-
include ':espresso:IntentsBasicSample:app'
6-
include ':uiautomator:BasicSample:app'
7-
include ':testrunner:AndroidJunitRunnerSample:app'
8-
include ':unittesting:BasicSample:app'
9-
include ':unittesting:BasicUnitAndroidTest:app'
10-
include ':integrationtesting:ServiceTestRuleSample:app'
11-
include ':espresso:IntentsAdvancedSample:app'
1+
include ':ui:espresso:BasicSample:app'
2+
include ':ui:espresso:CustomMatcherSample:app'
3+
include ':ui:espresso:MultiWindowSample:app'
4+
include ':ui:espresso:DataAdapterSample:app'
5+
include ':ui:espresso:IntentsBasicSample:app'
6+
include ':uiespresso:IntentsAdvancedSample:app'
7+
include ':ui:espresso:WebBasicSample:app'
8+
include ':ui:uiautomator:BasicSample:app'
9+
include ':runner:AndroidJunitRunnerSample:app'
10+
include ':unit:BasicSample:app'
11+
include ':unit:BasicUnitAndroidTest:app'
12+
include ':integration:ServiceTestRuleSample:app'
File renamed without changes.

ui/espresso/WebBasicSample/README.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 22
5+
buildToolsVersion '22.0.1'
6+
defaultConfig {
7+
applicationId "com.example.android.testing.espresso.web.BasicSample"
8+
minSdkVersion 10
9+
targetSdkVersion 22
10+
versionCode 1
11+
versionName "1.0"
12+
13+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
14+
}
15+
packagingOptions {
16+
exclude 'LICENSE.txt'
17+
}
18+
lintOptions {
19+
abortOnError false
20+
}
21+
productFlavors {
22+
}
23+
}
24+
25+
dependencies {
26+
// App dependencies
27+
compile 'com.android.support:support-annotations:22.2.0'
28+
compile 'com.google.guava:guava:18.0'
29+
// Testing-only dependencies
30+
androidTestCompile 'com.android.support.test:runner:0.3'
31+
androidTestCompile 'com.android.support.test:rules:0.3'
32+
androidTestCompile 'com.android.support.test.espresso:espresso-web:2.2'
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
* Copyright 2015, The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.android.testing.espresso.web.BasicSample;
18+
19+
import org.junit.Before;
20+
import org.junit.Rule;
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
24+
import android.content.Intent;
25+
import android.support.test.espresso.web.sugar.Web;
26+
import android.support.test.espresso.web.webdriver.DriverAtoms;
27+
import android.support.test.espresso.web.webdriver.Locator;
28+
import android.support.test.rule.ActivityTestRule;
29+
import android.support.test.runner.AndroidJUnit4;
30+
import android.test.ActivityInstrumentationTestCase2;
31+
import android.test.suitebuilder.annotation.LargeTest;
32+
import android.webkit.WebView;
33+
34+
import static android.support.test.espresso.web.assertion.WebViewAssertions.webMatches;
35+
import static android.support.test.espresso.web.sugar.Web.onWebView;
36+
import static android.support.test.espresso.web.webdriver.DriverAtoms.clearElement;
37+
import static android.support.test.espresso.web.webdriver.DriverAtoms.findElement;
38+
import static android.support.test.espresso.web.webdriver.DriverAtoms.getText;
39+
import static android.support.test.espresso.web.webdriver.DriverAtoms.webClick;
40+
import static org.hamcrest.Matchers.containsString;
41+
42+
/**
43+
* Basic sample that shows the usage of Espresso web showcasing {@link Web#onWebView()} API.
44+
* <p>
45+
* The sample has a simple layout which contains a single {@link WebView}. The HTML page displays
46+
* a form with an input tag and buttons to submit the form.
47+
*/
48+
@LargeTest
49+
@RunWith(AndroidJUnit4.class)
50+
public class WebViewActivityTest {
51+
52+
private static final String MACCHIATO = "Macchiato";
53+
private static final String DOPPIO = "Doppio";
54+
55+
/**
56+
* A JUnit {@link Rule @Rule} to launch your activity under test. This is a replacement
57+
* for {@link ActivityInstrumentationTestCase2}.
58+
* <p>
59+
* Rules are interceptors which are executed for each test method and will run before
60+
* any of your setup code in the {@link Before @Before} method.
61+
* <p>
62+
* {@link ActivityTestRule} will create and launch of the activity for you and also expose
63+
* the activity under test. To get a reference to the activity you can use
64+
* the {@link ActivityTestRule#getActivity()} method.
65+
*/
66+
@Rule
67+
public ActivityTestRule<WebViewActivity> mActivityRule = new ActivityTestRule<WebViewActivity>(
68+
WebViewActivity.class, false, false) {
69+
@Override
70+
protected void afterActivityLaunched() {
71+
// Technically we do not need to do this - WebViewActivity has javascript turned on.
72+
// Other WebViews in your app may have javascript turned off, however since the only way
73+
// to automate WebViews is through javascript, it must be enabled.
74+
onWebView().forceJavascriptEnabled();
75+
}
76+
};
77+
78+
@Test
79+
public void typeTextInInput_clickButton_SubmitsForm() {
80+
// Lazily launch the Activity with a custom start Intent per test
81+
mActivityRule.launchActivity(withWebFormIntent());
82+
83+
// Selects the WebView in your layout. If you have multiple WebViews you can also use a
84+
// matcher to select a given WebView, onWebView(withId(R.id.web_view)).
85+
onWebView()
86+
// Find the input element by ID
87+
.withElement(findElement(Locator.ID, "text_input"))
88+
// Clear previous input
89+
.perform(clearElement())
90+
// Enter text into the input element
91+
.perform(DriverAtoms.webKeys(MACCHIATO))
92+
// Find the submit button
93+
.withElement(findElement(Locator.ID, "submitBtn"))
94+
// Simulate a click via javascript
95+
.perform(webClick())
96+
// Find the response element by ID
97+
.withElement(findElement(Locator.ID, "response"))
98+
// Verify that the response page contains the entered text
99+
.check(webMatches(getText(), containsString(MACCHIATO)));
100+
}
101+
102+
@Test
103+
public void typeTextInInput_clickButton_ChangesText() {
104+
// Lazily launch the Activity with a custom start Intent per test
105+
mActivityRule.launchActivity(withWebFormIntent());
106+
107+
// Selects the WebView in your layout. If you have multiple WebViews you can also use a
108+
// matcher to select a given WebView, onWebView(withId(R.id.web_view)).
109+
onWebView()
110+
// Find the input element by ID
111+
.withElement(findElement(Locator.ID, "text_input"))
112+
// Clear previous input
113+
.perform(clearElement())
114+
// Enter text into the input element
115+
.perform(DriverAtoms.webKeys(DOPPIO))
116+
// Find the change text button.
117+
.withElement(findElement(Locator.ID, "changeTextBtn"))
118+
// Click on it.
119+
.perform(webClick())
120+
// Find the message element by ID
121+
.withElement(findElement(Locator.ID, "message"))
122+
// Verify that the text is displayed
123+
.check(webMatches(getText(), containsString(DOPPIO)));
124+
}
125+
126+
/**
127+
* @return start {@link Intent} for the simple web form URL.
128+
*/
129+
private static Intent withWebFormIntent() {
130+
Intent basicFormIntent = new Intent();
131+
basicFormIntent.putExtra(WebViewActivity.KEY_URL_TO_LOAD, WebViewActivity.WEB_FORM_URL);
132+
return basicFormIntent;
133+
}
134+
135+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
~ Copyright (C) 2015 The Android Open Source Project
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
19+
package="com.example.android.testing.espresso.web.BasicSample" >
20+
21+
<application
22+
android:icon="@drawable/ic_launcher"
23+
android:label="@string/app_name"
24+
android:theme="@style/AppTheme" >
25+
<activity
26+
android:name="com.example.android.testing.espresso.web.BasicSample.WebViewActivity"
27+
android:label="@string/app_name" >
28+
<intent-filter>
29+
<action android:name="android.intent.action.MAIN" />
30+
<category android:name="android.intent.category.LAUNCHER" />
31+
</intent-filter>
32+
</activity>
33+
</application>
34+
35+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<!--
2+
~ Copyright (C) 2015 The Android Open Source Project
3+
~
4+
~ Licensed under the Apache License, Version 2.0 (the "License");
5+
~ you may not use this file except in compliance with the License.
6+
~ You may obtain a copy of the License at
7+
~
8+
~ http://www.apache.org/licenses/LICENSE-2.0
9+
~
10+
~ Unless required by applicable law or agreed to in writing, software
11+
~ distributed under the License is distributed on an "AS IS" BASIS,
12+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
~ See the License for the specific language governing permissions and
14+
~ limitations under the License.
15+
-->
16+
<!doctype html>
17+
<html>
18+
<head>
19+
<title>Hello Espresso Web</title>
20+
21+
<meta charset="utf-8"/>
22+
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
23+
<meta name="viewport" content="width=device-width, initial-scale=1"/>
24+
<style type="text/css">
25+
body {
26+
background-color: #f0f0f2;
27+
margin: 0;
28+
padding: 0;
29+
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
30+
}
31+
div {
32+
width: 600px;
33+
margin: 5em auto;
34+
padding: 50px;
35+
background-color: #fff;
36+
border-radius: 1em;
37+
}
38+
#message {
39+
margin: 0 auto 12px auto;
40+
}
41+
a:link, a:visited {
42+
color: #38488f;
43+
text-decoration: none;
44+
}
45+
@media (max-width: 700px) {
46+
body {
47+
background-color: #fff;
48+
}
49+
div {
50+
width: auto;
51+
margin: 0 auto;
52+
border-radius: 0;
53+
padding: 1em;
54+
}
55+
}
56+
</style>
57+
</head>
58+
<body>
59+
<div>
60+
<h1>Hello Espresso Web!</h1>
61+
62+
<form action="web_form_response.html" method="GET">
63+
<p id="message">Need some Espresso</p>
64+
<input type="text" name="text_input" id="text_input" placeholder="type something"/><br>
65+
<input type="button" value="Change Text" onclick="changeText()" id="changeTextBtn"/><br>
66+
<input type="submit" value="Change Text And Submit" id="submitBtn"/><br>
67+
</form>
68+
</div>
69+
<script>
70+
function changeText() {
71+
var typedText = document.getElementById('text_input').value;
72+
document.getElementById('message').innerHTML=typedText;
73+
}
74+
</script>
75+
</body>
76+
</html>
77+

0 commit comments

Comments
 (0)