检查浏览器是否打开(uiautomator/espresso 测试)
Check that the browser was open (uiautomator/espresso test)
我使用 automator/espresso 测试来检查关注网络 link 并返回到应用程序:
@RunWith(AndroidJUnit4::class)
@SdkSuppress(minSdkVersion = 18)
class AboutApplicationActivityTest {
private lateinit var device: UiDevice
@Before
fun startMainActivityFromHomeScreen() {
device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
...
}
@Test
fun checkLink() {
val link: UiObject = device.findObject(UiSelector().resourceId("${BASIC_SAMPLE_PACKAGE}:id/link"))
link.click() // follow the link
/*
* How to check that the browser was open after
* clicking on the link? Which assertion can I use here?
*/
device.pressBack() // back to application
onView(withId(R.id.link))
.check(matches(isDisplayed())) // check return to application
}
}
如何在点击 link 后检查浏览器是否打开?我可以在这里使用哪个断言?
找到这个解决方案:
import com.google.common.truth.Truth.assertThat
@RunWith(AndroidJUnit4::class)
@SdkSuppress(minSdkVersion = 18)
class AboutApplicationActivityTest {
...
@Test
fun checkLink() {
...
link.click()
var currentPackage: String = device.currentPackageName
assertThat(currentPackage).isEqualTo("com.android.chrome")
device.pressBack()
...
}
}
我使用 automator/espresso 测试来检查关注网络 link 并返回到应用程序:
@RunWith(AndroidJUnit4::class)
@SdkSuppress(minSdkVersion = 18)
class AboutApplicationActivityTest {
private lateinit var device: UiDevice
@Before
fun startMainActivityFromHomeScreen() {
device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
...
}
@Test
fun checkLink() {
val link: UiObject = device.findObject(UiSelector().resourceId("${BASIC_SAMPLE_PACKAGE}:id/link"))
link.click() // follow the link
/*
* How to check that the browser was open after
* clicking on the link? Which assertion can I use here?
*/
device.pressBack() // back to application
onView(withId(R.id.link))
.check(matches(isDisplayed())) // check return to application
}
}
如何在点击 link 后检查浏览器是否打开?我可以在这里使用哪个断言?
找到这个解决方案:
import com.google.common.truth.Truth.assertThat
@RunWith(AndroidJUnit4::class)
@SdkSuppress(minSdkVersion = 18)
class AboutApplicationActivityTest {
...
@Test
fun checkLink() {
...
link.click()
var currentPackage: String = device.currentPackageName
assertThat(currentPackage).isEqualTo("com.android.chrome")
device.pressBack()
...
}
}