Android WebDriver. XMLHttpRequest cannot load "URL". The URL wire is not allowed by Access-Control-Allow-Origin. at zero value: 1

I run my automated tests on Nexus 10 (Adndroid 4.2) using Selenium WebDriver and Java. The error I am encountering is:

XMLHttpRequest cannot load 'URL1'. Origin 'URL2' is not allowed by Access- Control-Allow-Origin. at null:1 
  • URL1 - json based back-end;
  • URL2 - front-end.

This error does not occur for FireFox, Chrome, IE 10. But it always happens for WebDriver on Android. Because of this, login is not available. WebDriver can click, send Keys, etc.

So the question is: is there any workaround to avoid this problem? Could there be some settings that I have to change for WebDriver? Maybe someone has come across something similar before. I appreciate any suggestions.

I tried apk 2.21.0 and 2.32.0.

+4
source share
3 answers

In my case, it was a problem with web sockets. The application is based on web sockets. The fact is that the Android native browser does not support sockets until Android 4.4 and Selenium use their own Android browser to run tests.

I tested the same thing on an emulator with Android 4.4, and everything works fine. Thank you all for your help.

0
source

Take a look at this.

XMLHttpRequest cannot load 'URL1'. The origin of 'URL2' is not allowed by Access-Control-Allow-Origin. at zero value: 1

When you add the following code, the application will work correctly.

 if (Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) { webView.getSettings().setAllowUniversalAccessFromFileURLs(true); } 

So basically this happens when URL1 not available jquery, Json.

+3
source

As I don’t know your code for sure, I will give you only an explanation of how I think you could solve the problem, please do not consider this the real answer, just try :)

I sometimes had this problem, and usually you can fix it on your server using the CORS function, depending on the server application / structure you are using, you use this CORS differently, but basically you need to fill in the response header from the server following the relevant information:

 Access-Control-Allow-Origin Access-Control-Allow-Methods Access-Control-Allow-Headers 

A very "open" configuration will be:

 "Access-Control-Allow-Origin=* "Access-Control-Allow-Methods=GET, POST, PUT, DELETE, OPTIONS "Access-Control-Allow-Headers=Content-Type, Authorization 
+1
source

Source: https://habr.com/ru/post/1502761/


All Articles