Having some trouble when opening chrome browser with Selenium ChromeDriver - c#

I use Selenium ChromeDriver to open chrome browser and load a site into it programmatically.
I install selenium & ChromeDrive from this NuGet
Install-Package Selenium.WebDriver -Version 3.141.0 Install-Package
Selenium.WebDriver.ChromeDriver -Version 77.0.3865.4000
I have some questions:
if target pc has no chrome browser installed then how can i capture it by ChromeDriver ? is it possible?
when i am opening chrome browser by ChromeDriver instance then browser is opening chrome browser with a site but another CUI window is getting opened which i do not want to visible or i want to hide this CUI window. if it is not possible then how could i open this CUI window in minimize state?
a sample CUI window screen shot attached below when i work with FirefoxDriver. the same occur when i work with ChromeDriver instance.
when i executing this code chromeDriver.Close(); then opened chrome browser is getting closed but CUI window is still open. so if i click 5 times on open button then 5 CUI window is getting open along with 5 chrome browser instance which i had to close manually ....which i do not want to manually close it rather i want to close it when browser will be closed....how to achieve it ?
how to capture from code that opened chrome browser is close by this code chromeDriver.Close(); or if user click on cross button of chrome browser to close it?
how to open a new tab in already opened chrome browser instead of opening new chrome browser instance. if no chrome browser is open at all then new chrome browser will be open...how to achieve it by code. this below code opening new chrome browser always....what to change there for my point 5
chromeDriver = new FirefoxDriver(options);
chromeDriver.Navigate().GoToUrl("https://www.google.com");
another issue occur when i work with chrome driver that. it open chrome browser but a notification appear on browser like Chrome is being controlled by automated test software
I search google to hide it and found people said to use this option
options.setExperimentalOption("excludeSwitches", new String[] { "enable-automation" });
at my end this function does not available setExperimentalOption so what to do?
Please answer point wise with sample code.

For question 2,3 you can use below code
(use DriverService.Dispose(); to manually dispose driver service) :
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace MyProject
{
public class Browser : IDisposable
{
bool disposed = false;
IWebDriver Driver;
public Browser()
{
//Chrome Driver copied on startup path
ChromeDriverService driverService = ChromeDriverService.CreateDefaultService(Application.StartupPath, "chromedriver.exe");
//hide driver service command prompt window
driverService.HideCommandPromptWindow = true;
ChromeOptions options = new ChromeOptions();
//hide browser if you need
//options.AddArgument("headless");
//or this to hiding browser
//options.AddArgument("--window-position=-32000,-32000");
//On offer Dona bhatt for disable automated test notification
options.AddExcludedArgument("enable-automation");
//options.AddArgument("disable-infobars");
Driver = new ChromeDriver(driverService, options);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposed)
return;
if (disposing)
{
Driver.Close();
Driver.Quit();
Driver.Dispose();
DriverService.Dispose();
}
disposed = true;
}
//this method for navigation
public string Navigate(string url)
{
string page = string.Empty;
try
{
Driver.Navigate().GoToUrl(url);
page =Driver.PageSource;
}
catch
{
}
return page;
}
//this method for wait to an element be visible by element ID
private void WaitUntilLoad(string id, int timeOut)
{
WebDriverWait waitForElement = new WebDriverWait(Driver, TimeSpan.FromSeconds(timeOut));
try
{
waitForElement.Until(ExpectedConditions.ElementIsVisible(By.Id(id)));
}
catch (WebDriverTimeoutException e)
{
}
}
}
}
Use this class:
using(Browser brw=new Browser())
{
string pageSource=brw.Navigate("My URL");
}

I couldn't post this as a comment because it's too long. I think this question is off topic but I just noted my comments on your items...
This seems to be much to broad. Are you having a problem with any specific issue? All of these seems like things you can research and find out.
1) The chrome driver has to be downloaded and shipped with your app.
2) It might be possible to hide that window but not sure why that would be a hard requirement. I'm going to just say that you can't by default prevent the window from showing.
3) You're going to have to close the chrome windows through your .net code. Selenium isn't going to be graceful enough to terminate all the chrome windows.
4) I'm not sure what you're asking for. What's the problem?
5) I think tabs might be done very differently between browsers, it might not be something natively supported in selenium. There might be some chrome driver commands that can facilitate but I have no idea what they are.
6) Again, this is typically not the preferred experience, I'm guessing one of your packages has a different featureset than whatever you're reading was using.

Related

How can I force Selenium to control MiniBrowser?

I'm trying to automate some cross-browser website testing for my employer. Currently, we have a small C#-xUnit-Selenium testing suite for End-to-End tests. My goal is to run these tests locally on Windows using Google Chrome, Firefox, and Safari. The first two browsers I'm not worried about. Safari is giving me the trouble.
Since Safari doesn't support Win10, I've opted to use a WebKit-powered browser as a substitute. I know it won't be a 1-1 match, but I'm not concerned by the differences. My browser of choice is MiniBrowser, emulating Safari 15 with a very recent build of Webkit port WinCairo.
Here's my current problem: I don't know how to force Selenium to control MiniBrowser.
I would really like to have a class in Selenium that could parallel ChromeDriver() or SafariDriver() (WinCairoDriver(), I guess). But I don't know how to create it. I know that both WinCairo and MiniBrowser implement a WebDriver.exe file in their bin folders,
but I don't know how to translate that file into a driver class Selenium can recognize. I'm sure there are other requirements as well to get it working.
Any help would be appreciated.
Guide I used to build WinCairo browser
Link to my built WinCairo MiniBrowser
Here's where I would insert said WinCairoDriver() class if it existed:
namespace Project_Tests.Fixtures
{
public class DriverSetup : IDisposable
{
public IWebDriver driver;
public DriverSetup()
{
new DriverManager().SetUpDriver(new ChromeConfig());
            
var chromeDriver = new ChromeDriver();
            // I need help here:
//var wincairoDriver = new WinCairoDriver();
driver = chromeDriver; // Where I want to pass wincairoDriver;
}
public void Dispose()
{
driver.Quit();
}
}
}

how to get already opened IE browser handle in selenium?

I have one already opened IE browser , with some url.
After this, I Run below code which will open another IE browser. however it gives me only one window handle in below code.
Is it possible to get previously opened IE browser handle ?
IWebDriver IEdriver = new InternetExplorerDriver();
IReadOnlyCollection<String> browsers = IEdriver.WindowHandles;
foreach (String item in browsers)
{
IEdriver.SwitchTo().Window(item);
String url = IEdriver.Url;
}
I think this is what you're looking for:
String winHandleBefore = driver.getWindowHandle();
//Do whatever operations you have to do
for(String winHandle : IEdriver.getWindowHandles()){
IEdriver.switchTo().window(winHandle);
}
Be careful as what you are trying to do, is not robust solution to write tests cases. If one test case causes browser to crash you will be getting all the test cases failed.
Also I don't think it should be possible to get handles on previously opened window by default, because when you write code
IWebDriver IEdriver = new InternetExplorerDriver();
It calls for a constructor of InternetExplorerDriver class and opens new instance of Internet Explorer.
You can either go for close all browsers before starting your test case execution by killing the ie process from task manager.
foreach (Process process in Process.GetProcessesByName("iexplore"))
{
process.Kill();
}

Selenium IEDriver- Regarding PATH variable

I am working on creating a regression test suite using Selenium for IE Browser. I am using the IEDriver exe from Selenium website. As per instructions from Selenium,
"The Internet Explorer Driver Server
This is required if you want to make use of the latest and greatest features of the WebDriver InternetExplorerDriver. Please make sure that this is available on your $PATH (or %PATH% on Windows) in order for the IE Driver to work as expected."
Approach 1
I tried to setup PATH variable via batch file as follows
setlocal
set varC=%CD%\ChromeDriver
set varI=%CD%\IEDriver
set PATH=%PATH%;%varC%;%varI%
However i still face issues with IEDriver not working properly.
Approach 2
When i set PATH variable via "Advanced System Settings", everything seems to be working fine. Can someone confirm if this setting can't be done via batch file or if i am performing some wrong operation?
Here is how I am initializing driver
[OneTimeSetUp]
public void SetupTestFixture()
{
switch (ConfigPara.TestBrowser.ToLower())
{
case "ie":
Utility.KillProcess("iexplore");
DesiredCapabilities caps = DesiredCapabilities.InternetExplorer();
caps.SetCapability("ignoreZoomSetting", true);
caps.SetCapability("nativeEvents", false);
caps.SetCapability("allow-blocked-content", true);
caps.SetCapability("disable-popup-blocking", true);
caps.SetCapability("allowBlockedContent", true);
aOptIE = new OpenQA.Selenium.IE.InternetExplorerOptions();
aOptIE.InitialBrowserUrl = ConfigurationManager.AppSettings.Get("baseURL");
aOptIE.EnablePersistentHover = false;
aOptIE.RequireWindowFocus = true;
aOptIE.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
System.Environment.SetEnvironmentVariable("webdriver.ie.driver", ConfigPara.IEDriverDirectory.FullName +"\\IEDriverServer.exe");
Utility.Instance.driver = new InternetExplorerDriver(ConfigPara.IEDriverDirectory.FullName, aOptIE);
break;
}
Utility.Instance.driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(7));
baseURL = ConfigPara.BaseURL;
Utility.Instance.wait = new OpenQA.Selenium.Support.UI.WebDriverWait(Utility.Instance.driver, TimeSpan.FromSeconds(30));
//utility = new Utility(driver);
}
[OneTimeTearDown]
public void SetupTestTeardown()
{
try
{
Utility.Instance.driver.Quit();
Utility.Instance.driver.Dispose();
}
catch (Exception)
{
// Ignore errors if unable to close the browser
}
Assert.AreEqual("", verificationErrors.ToString());
}
Hi actually you have to set the path of the IE driver that you downloaded form here http://docs.seleniumhq.org/download/
please do it like below
System.setProperty("webdriver.ie.driver","pathofIEdriver\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
User below code for C# webdriver:
System.Environment.SetEnvironmentVariable("webdriver.ie.driver", "Path\IEDriverServer.exe");
You can use webdriver manager class to handle it.
WebDriverManager.iedriver().setup();
Added in your maven pom file :
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>3.8.1</version>
</dependency>
You can also use Webdrivermanage dependency for Gradle. This will automatically maintain IE driver exe file.

Selenium-WebDriver opened the new Firefox window, but didn’t navigate to URL

My source code is copied from selenium docs site. I didn’t make any change.
http://docs.seleniumhq.org/docs/03_webdriver.jsp#user-input-filling-in-forms
I installed Selenium library via NuGet, including
Selenium Remote Control(RC), Selenium WebDriver Mono, Selenium WebDriver Support Classes, Selenium WebDriver, and Selenium WebDriver-backed Selenium.
When I run this code, a new Firefox window was opened. But the Firefox doesn’t navigate to the URL, it just stuck, nothing was loaded.
I tried the Firefox v27, v29, v30 and v31, none of them worked.
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
// Requires reference to WebDriver.Support.dll
using OpenQA.Selenium.Support.UI;
class GoogleSuggest
{
static void Main(string[] args)
{
// Create a new instance of the Firefox driver.
// Notice that the remainder of the code relies on the interface,
// not the implementation.
// Further note that other drivers (InternetExplorerDriver,
// ChromeDriver, etc.) will require further configuration
// before this example will work. See the wiki pages for the
// individual drivers at http://code.google.com/p/selenium/wiki
// for further information.
IWebDriver driver = new FirefoxDriver();
//Notice navigation is slightly different than the Java version
//This is because 'get' is a keyword in C#
driver.Navigate().GoToUrl("http://www.google.com/");
// Find the text input element by its name
IWebElement query = driver.FindElement(By.Name("q"));
// Enter something to search for
query.SendKeys("Cheese");
// Now submit the form. WebDriver will find the form for us from the element
query.Submit();
// Google's search is rendered dynamically with JavaScript.
// Wait for the page to load, timeout after 10 seconds
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until((d) => { return d.Title.ToLower().StartsWith("cheese"); });
// Should see: "Cheese - Google Search"
System.Console.WriteLine("Page title is: " + driver.Title);
//Close the browser
driver.Quit();
}
}
I have had the same problem. the solution is simply uninstall your current firefox browser and download older version "i tried 2010 version 3.6.10 works great". your code is fine the problem is Mozilla people have decided to not give the right to any third party application to control Firefox.
good luck

Check if browser closed manually

In visual studio 2010, working with c#;
I open a browser with:
private IE browser;
private void Set_Browser()
{
string splashUrl = "google.com";
browser= new IE(splashUrl);
}
If a user(person) closes the browser by accident, then my application will not be able to work anymore.
QUESTIONS:
So how do I check if a user closed the browser manually?
Can I unable a user from closing the browser by adding the browser to my
application GUI as a control? [using Windows Forms]
-> How do I do that?
Last question related to this post How to use watin with WebBrowser control? (2 years old, but no decent answer too)
EDIT: The solution in give URL seems to work. Problem is that if I try to send the WebBrowser.ActivateX.. as an object to other class. Then my browser = new IE(..) returns null. It does work when I instantiate it in the form class though. Any solutions?
You can search for the process of internet explorer every x seconds and see if the browser is already running using this code:
bool isRunning = false;
foreach (Process clsProcess in Process.GetProcesses()) {
if (clsProcess.ProcessName.Contains("iexplore"))
{
isRunning = true;
break;
}
}
You can use this article
Or you can add a browser control to your application using this article
One thing you can do is hide the browser to avoid users closing it .. See this SO question.
Hiding Internet Explorer when WatiN is run

Categories

Resources