selenium user profile in headless mode, chrome browser - java

Situation: i'm running chrome in headless mode (see arguments in section part of code), when i connect to page (let say i sign into the page before i run my app that's using selenium) i'm already login to page (because it's using existing profile and i login to page before i started my app) that's on first computer, correct PROFILE is used.
On second computer i'm running Chrome with same settings (same as on computer one) and when i go to the same page as on computer one i'm NOT login to the page (because the profile with existing session is not used)...
If i remove the --headless option everything works... however i want to running it in HEADLESSS mode with current profile used by chrome.
Driver: Chrome driver version 101
Java version: 15
Browser: Google Chrome (version 101.0.4951.64)
Operating system: both computers have Windows 10
Part of code:
String userP=getChromeUserProfilePath();
options.addArguments(String.format("--user-data-dir=%s",userP));
if(getChromeUserProfileName()!=null){
options.addArguments(String.format("--profile-directory=%s",getChromeUserProfileName()));
}
options.addArguments("--headless",
"--disable-gpu",
"--window-size=1920,1200",
"--ignore-certificate-errors",
"--disable-extensions",
"--no-sandbox",
"--disable-dev-shm-usage");
ChromeDriver webDriver = new ChromeDriver(options);
Questions:
Where's the catch ? How do i make it work for second computer?

Related

Issue while opening headless chrome in remote jenkins server using selenium java

I am having trouble in using headless chrome on jenkins remote server. the test is failed at the time of url invoking or takes to much of a time like 5+mins and test failed as it wont have waiting time for that long, i have tried different chromeoption like disable gpu, nosandbox etc still the issue is not solved.There is no issue in opening those url in normal mode but its not working on headless mode. i am using this chromeoptions for test:
enter code here
``` chromeOptions.addArguments("--window-size=1366,768");
chromeOptions.addArguments("--disable-gpu");
chromeOptions.addArguments("--disable-extensions");
chromeOptions.addArguments("--proxy-server='direct://'");
chromeOptions.addArguments("--proxy-bypass-list=*");
chromeOptions.addArguments("--start-maximized");
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("no-sandbox", "--disable-infobars", "--disable-dev-shm-usage", "--disable-browser-side-navigation",
"--ignore-certificate-errors");```

How to automate the mobile emulation in chrome browser in browserstack?

I'm trying to automate the mobile emulation in chrome browser. So I have done in local chrome execution successfully, not sure about the browserstack chrome mobile emulators. I just wanted to know how should we do the same in browserstack.
Similar to local chrome execution, just have to open a chrome browser with the given chrome options(android,ipad,iphone) in browserstack.
https://chromedriver.chromium.org/mobile-emulation
Local chrome Execution code snippet:
`"chromeAndroid": {
"deviceName": "Nexus 5"
},
"chromeiPhone": {
"deviceName": "iPhone 5"
},
"chromeiPad": {
"deviceName": "iPad"
},
Map<String, String> mobileEmulation = new HashMap<>();
mobileEmulation.put(pair.getKey().toString(), pair.getValue().toString());
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation);
driver = new ChromeDriver(chromeOptions);`
We are parsing the data from above json file and pass it onto the chrome options.
Local chrome execution Screenshot:
Please suggest me by providing some details or code snippet for remoteWebdriver as we are running the browser stack tests from Jenkins and browserstack local.
Please help me!
What do you mean by the "mobile emulation in chrome browser" in browserstack? If it's a desktop Chrome with the mobile emulation enabled (according to your code) then everything should work the same as locally.
If you are testing Chrome in a physical device in browserstack then you need to pass a few more capabilities like
realMobile - should be set to true
device - specify a device
os_version - OS version of a device you want to test
browserstack.appium_version - Appium version
You can find more by the link
Also, here is a guide on Selenium tests with Chrome

In parallel execution, safari browser navigate to url with out login page automatically

I am using selenium web driver 2.48 and safari driver 2.48 and safari version 8.0.8
I am facing a problem on running my test execution in safari driver.The problem is
"In parallel execution, if in one safari window,login is successfull, than in other
safari windows, this login page is not shown,
that means safri navigate to url with
out login as one safari window already complete login."
and for this reason, i am facing below issue in parallel execution:
"CSRF verification failed.Request aborted"
I want like that for parallel execution:
if five safari browser window open, in each window, login page will be appeared.
In a sense, each safari driver instance will not share other safari driver
instances resources or any other thing
I have changed safari preferences settings but its not helping.
Is there any best way to declare safari driver or i need to add any desired capabilities or
any other things in safari preferences or any any good suggestion.
please and thanks.
i am using the following code:
SafariOptions safariOptions = new SafariOptions();
safariOptions.setUseCleanSession(true);
DesiredCapabilities dc = DesiredCapabilities.safari();
dc.setCapability(SafariOptions.CAPABILITY, safariOptions);
currentDriver = new SafariDriver(dc);
Set<Cookie> cookies = currentDriver.manage().getCookies();
currentDriver.manage().deleteAllCookies();
if(!cookies.isEmpty())
{
Iterator<Cookie> iter= currentDriver.manage().getCookies().iterator();
while(iter.hasNext()){
Cookie C = iter.next();
}
cookies.clear();
}
I personally have not used safari before. And as you said that the behavior in all other browsers are as you expect it to be. I would like you to try the following.
There is a way by which you can specify the profile of the driver instance to be used. I would suggest you to create separate profiles for each instance and try out . How to do that is explained in this answer. This answer explains how to use same profile each time . You need to use a different profile each time. ( Which happens by default in chrome not sure why it wouldn't for safari )

Set Jenkins Windows Slave Screen Resolution For Selenium Tests

I got a Jenkins windows 7 slave for testing my UI webapp using Selenium WebDriver (java, with Firefox and chrome drivers; machine is vmware vm).
Also, slave is connected to jenkins master using JNLP agent.
Problem is: Tests fail on slave, since resolution seems to be 800x600 whereas I need 1920x1080-ish.
Setting selenium web driver windows size won't do it, i.e.
driver.manage().window().setSize(new Dimension(1920, 1080));
driver.manage().window().maximize();
Any ideas? I'm trying to figure out where display resolution configuration should be done. Should it be done on JVM side (as test is execute by jenkins slave)? Or is it a windows issue?
We had a similar issue and we resolved it by setting up the Jenkins slave on windows an admin session of RDP started from a PC with a 1920x1080 resolution screen.
Start -> run -> mstsc /admin
This opens the Jenkins slave in the desired resolution.
Then, you just disconnect the RDP session and this leaves the session running in the desired resolution.
I hope this helps.
The display setup should be a one time activity performed on slave. You will have to remote into slave as admin using RDP to set resolution.
I had similar issue, then I used Phantom JS driver instead of Chrome
try this:
driver.manage().window().maximize();
driver.manage().window().setSize(new Dimension(1936, 1176));
Problem resolved from my end
Solution that worked for me is to run Chrome in a 'headless' mode (without GUI). It works with Jenkins Agent running as a service, when GUI is not available.
Here is the code sample of the web driver initialization (C#, I guess will be similar in java):
var options = new ChromeOptions();
options.BinaryLocation = #"C:\Program Files\Google\Chrome\Application\chrome.exe";
options.AddArgument("Headless");
options.AddArgument("window-size=1920,1080");
driver = new ChromeDriver(#"<path>\Selenium.WebDriver.ChromeDriver.2.37.0\driver\win32", options);

Selenium tests does not run when Internet explorer 9 or Virtual machine is minimised

Internet Explorer - version 9
Selenium - 2.42.2
language - JAVA
What steps will reproduce the problem?
1.Trigger a login test on windows VM on IE with the following capabalities:
InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS=true
InternetExplorerDriver.IGNORE_ZOOM_SETTING=true
InternetExplorerDriver.REQUIRE_WINDOW_FOCUS=true
InternetExplorerDriver.ENABLE_PERSISTENT_HOVERING=true
InternetExplorerDriver.NATIVE_EVENTS=false
InternetExplorerDriver.INITIAL_BROWSER_URL="https://***********.com
2.Minimise the VM and
3.Wait for the selenium script to run and then maximise the VM to check if the tests are running fine.
What is the expected output? What do you see instead?
Expected Output: The login to the application should be completed.
Instead, the test is stuck in the login page. The user-id and password is not entered.(The webdriver is unable to identify the element)
Selenium version:2.42.2
OS:Windows 7
Browser:Internet Explorer
Browser version:9
The tests are triggered from jenkins server remotely. The tests run fine when it is in the foreground, but fails if the VM or IE is minimised.
If you are able to change the Protected Mode settings for each zone to same value (details are here). Then don't set following capability -
InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS=true
And also try without setting following capabilities (make sure zoom level set to 100%) -
InternetExplorerDriver.IGNORE_ZOOM_SETTING=true
InternetExplorerDriver.REQUIRE_WINDOW_FOCUS=true

Categories

Resources