Getting the Bluetooth Pairing Code on Appium

Viewed 23

I am trying to automate testing an Android app using Appium that searches for a specific device that is advertising BLE and tries to pair to it. Once the app detects the device, the Bluetooth pairing code will appear on the top. Using Appium Inspector, the accessibility ID for this pairing code shows up as "NavBar_Title" with element text of "Code: 186100" from this screenshot. Knowing that, I attempt to copy this code using GetAttribute("text") but run into the following error:

OpenQA.Selenium.StaleElementReferenceException: 'The element 'By.accessibilityId: NavBar_Title' does not exist in DOM anymore'.

Here's my code in C#:

WebDriverWait wait = new(driver, TimeSpan.FromSeconds(30)); //wait 30 second timeout
public string codeBLE; //initial text from NavBar_Title for Bluetooth pairing code
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(MobileBy.AccessibilityId("PairDevice_PairNowButton"))).Click(); //explicit wait for pair button, then click
//Bluetooth device begins advertising
codeBLE = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(MobileBy.AccessibilityId("NavBar_Title"))).GetAttribute("text"); //explicit wait for pairing code
//trim pairing code to 186100 to copy and paste

Other notes: I am running Android 9.0, and the Bluetooth pairing request textbox doesn't show up until the notification is clicked.

0 Answers
Related