Page tree
Skip to end of metadata
Go to start of metadata

Last updated: Feb 12, 2021 20:37

Problem:

There are too many tabs open in Google Chrome on Android OS. This can cause automation problems.

How to Resolve:

Close the browser tabs. This can be done several ways.

Solution #1: Using the built in menu of Google Chrome to Close all Tabs

"Close all tabs" menu can be accessed as follows:

1. Select list tabs button:
In automation script , you can push this button using the following object:
Element identifier = "com.android.chrome:id/tab_switcher_button"
By = "Id"

List tabs button



2. Click the menu button
Element identifier = "com.android.chrome:id/menu_button"
By = "Id"


3. Click the Close all tabs button 
Element identifier = "Close all tabs"
By = "Id"

Close all tabs


Solution #2: Using Automation to Close All Browser Tabs in Google Chrome for Android

It is entirely possible to automate the closing of Google Chrome's tabs, in android OS. Here is a code sample that explains how this can be achieved.

Note: This code sample is using our reportium client. If you are not using reporting, you can remove the reportiumClient syntax.

Important: Please be aware that when working with the Menus or Tab Switch controls of Google Chrome, this is in NATIVE context, not WEBVIEW/DOM. This code directly works with the Browser App itself, not the web content inside the browser.

Code Sample - Close All Chrome Browser Tabs
String myAUT = "com.android.chrome";
String xpath1 = "//*[@resource-id=\"com.android.chrome:id/tab_switcher_button\"]";
String xpath2 = "//*[@resource-id=\"com.android.chrome:id/menu_button_wrapper\"]";
String xpath3 = "//*[@content-desc=\"Close all tabs\"]";

reportiumClient.stepStart("Open My Browser");
params.clear();
params.put("identifier", myAUT);
driver.executeScript("mobile:application:open", params);

reportiumClient.stepStart("Click Tab Button");
driver.context("NATIVE");
driver.findElementByXPath(xpath1).click();

reportiumClient.stepStart("Open Chrome Menu");
driver.findElementByXPath(xpath2).click();

reportiumClient.stepStart("Click Close all tabs");
driver.findElementByXPath(xpath3).click();

Updated by Christopher Alton (02-12-2021)