I'm using TFS 2015 to trigger Selenium automated test suite. Few of test cases are failing due to some HTML elements get overlapped and this happens due to the screen resolution of the session which TFS service account opens is smaller (Programmatically I took screenshots while running the test methods in TFS). All the test cases are passing when I run from Visual Studio or through CMD locally or Remote Desktop session in TFS server.
I tried increasing the screen resolution using bellow techniques , but still screenshots saved in TFS server shows smaller screen size.
Increase the window size :
driver.Manage().Window.Size = new Size(x, y);
Chrome capabilities - Resolution
DesiredCapabilities caps = new DesiredCapabilities();
caps.SetCapability("resolution", "1920x1080");
Chrome capabilities - Window Size
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.AddArgument("--window-size=1300,1000");
Maximize the window
driver.manage.window.maximize();
None of these approaches works since the screen resolution of the session which TFS service account opens is smaller.
When I run the test cases with Headless option, browser windows opens with Maximum window size but test fails since selenium can't identify the elements (NoSuchElementException) :
var chromeOptions = new ChromeOptions();
chromeOptions.AddArguments("headless");
(Project which I'm automating is a ReactJS project)
I would like to know a way to increase the screen size when test are being executed form TFS agent.
Thank You
chromeOptions.addArgument("--headless");
chromeOptions.addArgument("--disable-gpu");
chromeOptions.addArgument("--window-size=1920,1080")
WebDriver driver = new ChromeDriver(chromeOptions);
don not use browser.maximize after this it will reset the windows size to system default.
Try also:
chromeOptions.addArguments("--headless","--disable-gpu","--window-size=1920,1080")
WebDriver driver = new ChromeDriver(chromeOptions);
Debugging tips
See if the test are passing with same window size but non headless mode
If it does pass then it is because of faster execution speed , headless browser is faster than non headless browser so you need to add more explicit waits for those elements
Related
im looking example how can i start my normal google chrome in webdriver c#?
For now i use :
ChromeDriver driver;
public ChromeDriverService chromeDriverService;
chromeDriverService = ChromeDriverService.CreateDefaultService();
chromeDriverService.HideCommandPromptWindow = true;
var chromeOptions = new ChromeOptions();
chromeOptions.AddArguments("disable-infobars");
chromeOptions.AddExcludedArgument("enable-automation");
chromeOptions.AddAdditionalCapability("useAutomationExtension", false);
driver = new ChromeDriver(chromeDriverService,chromeOptions);
But it run my chromedriver.exe installed inside project. Can i just run my simple installed chrome? Without download any chromedriver.exe? It's important for me , because some website check if there is opened chromium with chromedriver.exe.
Is it possible to do that?
If you want not just open it but also perform some interaction with your site then you have to deal with Selenium and WebDriver.
You can try Python and undetected-chromedriver which has some workarounds preventing some systems to detect that your browser is running under webdriver control.
Unfortunately, it's the chromedriver.exe that lets you interact with Chrome so, without the driver, you wouldn't be able to interact with the actual browser.
If your issue is with chromium, try driving Mozilla with GeckoDriver
Selenium always uses chromedriver.exe for interacting with Chrome, I think you mean you want to load your default data directory. You can add a chrome option for this purpose:
chromeOptions.AddArguments(#"C:\Users\<your user name>\AppData\Local\Google\Chrome\User Data")
Take care, any other chrome must not be running that time with the same data directory. Like you may have run for your personal use.
You can also create a data directory somewhere else if you just want to show some realistic behavior to a website. But some websites are smart enough so it does not work every time.
I'm using the C# Selenium and Edge webdriver on Windows 10. Below is my code for switch between tab.
public class BrowserTest
{
private readonly EdgeDriver driver;
public BrowserTest()
{
EdgeDriverService service = EdgeDriverService.CreateDefaultService(configuration[DriverPathKey]);
service.SuppressInitialDiagnosticInformation = true;
service.HideCommandPromptWindow = true;
driver = new EdgeDriver(service, new EdgeOptions()
{
PageLoadStrategy = PageLoadStrategy.Default,
});
}
public void OpenGoogleAndDuckduckgo()
{
driver.Url = "https://google.com/";
driver.ExecuteScript("window.open();");
driver.SwitchTo().Window(driver.WindowHandles.Last()); // This line lead to Edge focusing
driver.Url = "https://duckduckgo.com/";
}
}
But seem it focusing Edge browser that taking me away from checking my e-mail or etc.
How can I get Selenium to "silently" focus on a new tab (or window etc) so I can do something while my code runs?
P/s: I tried for headless mode but I need to manual input credentials to login so I need a UI. Or if there a way to turn from window mode to headless mode in runtime?
You can not achieve that (change focus without focus) in Selenium. If you are running Selenium tests, just use different machine. HW or virtual.
Headless mode of browser is not a good solution also. Sooner or later you will have too much problems to solve with differences between normal mode and headless mode.
Avoiding captcha is very hard in automation mode of browser. You need AI to solve captcha in that mode and it is even not guaranteed it will works (depends on captcha and server protection).
If you need to test or grab some pages which has this protection you have to use different approach: Write addon for browser (javascript app) and run it in regular mode of browser.
I think you should use the same code but consider deploying your script in VM. with that you can continue to do your stuff in your local wherein your selenium code will get executed in Virtual machine (With your own OS setup )
Oracle VM virtual Box provides very feasible solution with respect to virtual machines. Check out their official web sites linked. They have different distribution for MacOS, Linux and Windows operating system.
I have tried
options.add_argument("--headless")
driver = new ChromeDriver(options)
it's working fine but my requirement is to hide it after initialization.
As you can see here: How to execute tests with selenium webdriver while browser is minimized there is no way for the browser to be minified.
You could in theory move the browser out of view as described here https://sqa.stackexchange.com/questions/15484/how-to-minimize-the-browser-window-which-was-launched-via-selenium-webdriver
driver.manage().window().setPosition(new Point(-2000, 0))
Chrome headless is an instance of chrome. You can't start from normal mode and switch to headless mid test. Once you start testing with any kind of browser mode, you need to stick to it. There are some work arounds I've not tried with opening windows with code and continuing from the new window.
Use phantomJSDriver for Headless automation
WebDriver driver = new PhantomJSDriver();
I'm using selenium webdriver to load and take the screenshot of a webpage at regular intervals this way:
System.Drawing.Bitmap screenshot = new System.Drawing.Bitmap(new System.IO.MemoryStream(byteArray));
however, every time this line gets called, the chrome window becomes maximized and in the way. Is there a way to have this happen in the background without chrome popping up all the time?
Yes, running Chrome headless.
Starting from version 59 for Mac and version 60 for Windows it’s possible to run Chrome in headless mode, effectively hiding the browser window.
using OpenQA.Selenium.Chrome;
...
// Create a browser with BrowserOptions
var options = new ChromeOptions();
options.AddArgument("headless");
var driver = ChromeDriver(options);
// Do your other Selenium stuff here
// Take your screenshot
Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
ss.SaveAsFile(fileName, imageFormat);
I am using selenium WD in C# for cross browser testing but facing a strange problem that when ever i run my test using Nunit firstly Firefox window will open & then my desired browser window will open & run the test on it(desired browser).
As per my knowledge if any system is not having Firefox installed in it then it fails the script.
So is there any way to change this default value of browser in selenium.
I am able to run tests on different browser, my problem is only that before opening my desired browser by default first system is opening firefox. which create issue for me & my tests.
public void SetupTest()
{
driver = new SafariDriver();
baseURL = "http://google.com/";
verificationErrors = new StringBuilder();
}
Most probably, somewhere in your code you are initializing the Firefox Driver. Search for this within you code:
new FirefoxDriver();
You could also debug to the line
driver = new SafariDriver();
and see if it has an assigned value already.
But I am also pretty certain that you are initializing a FirefoxDriver somewhere.