要针对Chrome自定义标签进行Android Espresso测试,可以按照以下步骤进行操作:
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation 'androidx.test.espresso:espresso-web:3.4.0'
import androidx.test.espresso.Espresso;
import androidx.test.espresso.matcher.ViewMatchers;
import androidx.test.espresso.web.matcher.DomMatchers;
import androidx.test.espresso.web.sugar.Web;
import androidx.test.espresso.web.webdriver.DriverAtoms;
import androidx.test.espresso.web.webdriver.Locator;
import androidx.test.ext.junit.rules.ActivityScenarioRule;
import androidx.test.filters.LargeTest;
import androidx.test.platform.app.InstrumentationRegistry;
import org.junit.Rule;
import org.junit.Test;
import static androidx.test.espresso.web.assertion.WebViewAssertions.webMatches;
@LargeTest
public class ChromeCustomTabTest {
@Rule
public ActivityScenarioRule activityRule =
new ActivityScenarioRule<>(MainActivity.class);
@Test
public void testCustomTab() {
// 在Custom Tab中加载URL
Espresso.onView(ViewMatchers.withId(R.id.customTabButton)).perform(ViewActions.click());
// 等待Custom Tab加载完成
Espresso.onView(ViewMatchers.isAssignableFrom(WebView.class))
.check(webMatches(DomMatchers.containingTextInBody("Hello World")));
// 执行自定义操作,例如点击一个元素
Web.onWebView()
.withElement(DriverAtoms.findElement(Locator.LinkText, "Click Me"))
.perform(DriverAtoms.webClick());
}
}
public class MainActivity extends AppCompatActivity {
// ...
public void openCustomTab(View view) {
String url = "https://example.com";
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(this, Uri.parse(url));
}
}
请注意,上述示例中的代码可能需要根据你的项目的具体需求进行调整。确保你已经正确导入所需的类和资源,并且自定义操作与你的Custom Tab中的元素相匹配。