How to get Chrome culture?

Ask general questions here.
mrt
Posts: 259
Joined: Mon Mar 16, 2020 11:31 am

How to get Chrome culture?

Post by mrt » Thu Aug 04, 2022 9:41 am

Dear all,

how is it possible to get the current chrome localization?

Scenario:
The input field expects a date format entered in the current Chrome language, e.g.
English: 08/04/2022
German: 04.08.2022
otherwise it throws an error.

So what I need is to get the current Chrome language setting, to be then able to parse the date value from a data source into the expected format before entering it.

Using

Code: Select all

CultureInfo.CurrentCulture;
or

Code: Select all

CultureInfo.CurrentUICulture;
always returns the culture of the machine itself, not the one from the browser.

any idea?
thanks!

csaszi89
Posts: 42
Joined: Mon Jan 17, 2022 12:10 pm

Re: How to get Chrome culture?

Post by csaszi89 » Thu Aug 04, 2022 12:43 pm

Hi mrt,

you can call some javascript:

Code: Select all

var repo = yourRepo.Instance;
var webDocument = repo.yourApp.Self;
string script = "return navigator.language;";
string lang = webDocument.ExecuteScript(script);
Report.Info(lang); // en-EN
Greetings,
Gyuri

mrt
Posts: 259
Joined: Mon Mar 16, 2020 11:31 am

Re: How to get Chrome culture?

Post by mrt » Thu Aug 04, 2022 12:43 pm

Found it using javascript, but if anyone has an idea how to do it without JS it would be very appreciated.

JS solution:

Code: Select all

WebDocument webDoc = webDocumentInfo.CreateAdapter<WebDocument>(true);
string language = webDoc.ExecuteScript("return window.navigator.language");
Edit:
Sorry, didnt refresh the page before posting my answer.
Thanks anyway! :)

csaszi89
Posts: 42
Joined: Mon Jan 17, 2022 12:10 pm

Re: How to get Chrome culture?

Post by csaszi89 » Thu Aug 04, 2022 1:04 pm

Hi,

if you want to avoid using javascript, you can ask your developers to embed the culture into one of the elements of the DOM (or even for the input fields).
I mean, they can probably create a custom attribute for input fields (eg. culture="en-EN").
Perhaps, they can somehow provide this information, then you can find the element with Ranorex and simply read the culture with GetAttributeValue method.
Theoretically, it should work, but I am not sure! :wink:

Greetings,
Gyuri

mrt
Posts: 259
Joined: Mon Mar 16, 2020 11:31 am

Re: How to get Chrome culture?

Post by mrt » Thu Aug 04, 2022 1:48 pm

Hey Gyuri,

in theory, you are absolutely right.

In practice, it is very, very hard to convince devs to changes something, especially if the information is provided "somewhere else".
Especially especially when they develop in javascript. ;)

I will stick to the JS implementation for now, thanks anyways. :)