2
votes

I am using selenium and appium to automate mobile chrome browser on Android phone. I have connected real android device through

adb connect {ip address of mobile}

All other test cases on android chrome browser on a real device are running successfully. But for the image upload, the browser is crashing.

I am using below code for uploading image

 public class UploadPhotoPage extends TestBase {

    // Page Factory Object Repository
    @FindBy(id = "file-upload")
    WebElement fileUpload;

    // Initialising the page object
    public UploadPhotoPage() {
    PageFactory.initElements(driver, this);
    }

    // Actions
    public void uploadphotos(String imagePath) throws AWTException, InterruptedException {
    fileUpload.sendKeys(imagePath);
    }
}

Image path is on windows machine where appium server and eclipse are executing test cases.

What is wrong here?

Error log on appium server:

[W3C] Driver proxy active, passing request on via HTTP proxy [JSONWP Proxy] Matched '/wd/hub/session/34e405a5-1bad-4711-9e1c-956e2d7391f6/element/0.6272871137575717-18/text' to command name 'getText' [JSONWP Proxy] Proxying [GET /wd/hub/session/34e405a5-1bad-4711-9e1c-956e2d7391f6/element/0.6272871137575717-18/text] to [GET http://127.0.0.1:8003/wd/hub/session/adb1415c77099095355e619b36dc5e1e/element/0.6272871137575717-18/text] with body: {} [JSONWP Proxy] Got response with status 200: "{\"sessionId\":\"adb1415c77099095355e619b36dc5e1e\",\"status\":10,\"value\":{\"message\":\"stale element reference: element is not attached to the page document\n (Session info: chrome=70.0.3538.64)\n (Driver info: chromedriver=2.42.591088 (7b2b2dca23cca0862f674758c9a3933e685c27d5),platform=Windows NT 6.1.7601 SP1 x86_64)\"}}" [W3C] Encountered internal error running command: ProxyRequestError: Could not proxy command to remote server. Original error: The request to /wd/hub/session/34e405a5-1bad-4711-9e1c-956e2d7391f6/element/0.6272871137575717-18/text has failed [W3C] at JWProxy.proxy$ (C:\Users\chintamanim\AppData\Local\Programs\Appium\resources\app\node_modules\appium\node_modules\appium-base-driver\lib\jsonwp-proxy\proxy.js:182:13) [W3C] at tryCatch (C:\Users\chints\AppData\Local\Programs\Appium\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:67:40) [W3C] at GeneratorFunctionPrototype.invoke [as _invoke] (C:\Users\chints\AppData\Local\Programs\Appium\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:315:22) [W3C] at GeneratorFunctionPrototype.prototype.(anonymous function) [as next] (C:\Users\chints\AppData\Local\Programs\Appium\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:100:21) [W3C] at GeneratorFunctionPrototype.invoke (C:\Users\chints\AppData\Local\Programs\Appium\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:136:37) [W3C] at [MJSONWP] Matched JSONWP error code 10 to StaleElementReferenceError [HTTP] <-- GET /wd/hub/session/34e405a5-1bad-4711-9e1c-956e2d7391f6/element/0.6272871137575717-18/text 404 95 ms - 2004 [HTTP] [HTTP] --> GET /wd/hub/session/34e405a5-1bad-4711-9e1c-956e2d7391f6/element/0.6272871137575717-18/text [HTTP] {} [W3C] Driver proxy active, passing request on via HTTP proxy [JSONWP Proxy] Matched '/wd/hub/session/34e405a5-1bad-4711-9e1c-956e2d7391f6/element/0.6272871137575717-18/text' to command name 'getText' [JSONWP Proxy] Proxying [GET /wd/hub/session/34e405a5-1bad-4711-9e1c-956e2d7391f6/element/0.6272871137575717-18/text] to [GET http://127.0.0.1:8003/wd/hub/session/adb1415c77099095355e619b36dc5e1e/element/0.6272871137575717-18/text] with body: {} [JSONWP Proxy] Got response with status 200: "{\"sessionId\":\"adb1415c77099095355e619b36dc5e1e\",\"status\":10,\"value\":{\"message\":\"stale element reference: element is not attached to the page document\n (Session info: chrome=70.0.3538.64)\n (Driver info: chromedriver=2.42.591088 (7b2b2dca23cca0862f674758c9a3933e685c27d5),platform=Windows NT 6.1.7601 SP1 x86_64)\"}}" [W3C] Encountered internal error running command: ProxyRequestError: Could not proxy command to remote server. Original error: The request to /wd/hub/session/34e405a5-1bad-4711-9e1c-956e2d7391f6/element/0.6272871137575717-18/text has failed [W3C] at JWProxy.proxy$ (C:\Users\chints\AppData\Local\Programs\Appium\resources\app\node_modules\appium\node_modules\appium-base-driver\lib\jsonwp-proxy\proxy.js:182:13) [W3C] at tryCatch (C:\Users\chints\AppData\Local\Programs\Appium\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:67:40) [W3C] at GeneratorFunctionPrototype.invoke [as _invoke] (C:\Users\chints\AppData\Local\Programs\Appium\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:315:22) [W3C] at GeneratorFunctionPrototype.prototype.(anonymous function) [as next] (C:\Users\chints\AppData\Local\Programs\Appium\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:100:21) [W3C] at GeneratorFunctionPrototype.invoke (C:\Users\chints\AppData\Local\Programs\Appium\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:136:37) [W3C] at [MJSONWP] Matched JSONWP error code 10 to StaleElementReferenceError [HTTP] <-- GET /wd/hub/session/34e405a5-1bad-4711-9e1c-956e2d7391f6/element/0.6272871137575717-18/text 404 135 ms - 2004

Below are the capabilities, I have set

        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
        capabilities.setCapability("deviceName", "ONEPLUS A5010");
        capabilities.setCapability("platformVersion", "8.1.0");
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("browserName", "Chrome");
        capabilities.setCapability("deviceId", "XXX.XX.XX.XXX:5555");
        capabilities.setCapability("autoGrantPermissions", true);
2
You need to show more code in your original post. You show declaring (but not initializing) a webelement and a sendkeys but nothing else. A stale element message generally means that the web document has changed since the last time the element was assigned a value. It might help if you were to include a snippet of the HTML code as well.Bill Hileman
Thanks for the comment Sir. I have edited the post as per your suggestion.Chintamani Manjare
Other than perhaps adding AppiumFieldDecorator to your page initialization to be able to define a timeout, I would suggest that you create a new page object immediately before attempting to call your uploadphotos method - in other words I'm assuming you already created an instance and were doing other things before calling that method, but something is changing and to work around a stale element, re-create the page object itself, then call your uploadphotos.Bill Hileman
On second thought, yours is not a native app, but a browser, so instead of AppiumFieldDecorator, you'd want to use something like AjaxElementLocatorFactoryBill Hileman
@BillHileman Your suggestion of creating new page object worked to remove stale element exception. But it was not the root cause for upload image issue. We were mixing two issues due to logs. Thanks for the help.Chintamani Manjare

2 Answers

2
votes

The issues with my script are as below 1) Stale element issue is because of another element so it was not the root cause. 2) The image path should be of the mobile image not on the desktop. It was a major issue.

0
votes

One quick solution to handle StaleElementException is to again find the same element in DOM as soon as we encounter this Exception for that WebElement

 public class UploadPhotoPage extends TestBase {

// Page Factory Object Repository
@FindBy(id = "file-upload")
WebElement fileUpload;

// Initialising the page object
public UploadPhotoPage() {
PageFactory.initElements(driver, this);
}

// Actions
public void uploadphotos(String imagePath) throws AWTException, InterruptedException {
try {
fileUpload.sendKeys(imagePath);
}
catch(StaleElementException ste){
driver.findElement(By.id("file-upload")).sendKeys(imagePath);
}
    }
}