Selenium Webdriver and Embedded Browser in a Winform - How to connect web_driver instance? - selenium

I need to connect/create an instance of Selenium Webdriver to web content that my application under test opens in a Winform.
My test framework opens the Winform application with CodedUI, and further test steps cause the Winform application to open another Winform window that has a browser embedded in it. I need to create or tie a Selenium instance to this embedded browser content. It's using WebViewer/Edge in the embedded area.
Note - Not yet ready to move to WinAppDriver, if ever.

Resolved this. I found out that the web pane is an object called a WebView2.
Automating inside it requires the application to be launched in a special way:
Dim AppPath As String = "[full path to your app and its name].exe"
Dim edgeOptions As New EdgeOptions
edgeOptions.UseChromium = True
edgeOptions.UseWebView = True
edgeOptions.DebuggerAddress = "localhost:9222"
Dim AppStartInfo = New ProcessStartInfo(AppPath)
AppStartInfo.UseShellExecute = False
AppStartInfo.EnvironmentVariables("WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS") = "--remote-debugging-port=9222"
Diagnostics.Process.Start(AppStartInfo)
You also need:
Selenium Support and Selenium Webdriver version 4.0.0-rc3 installed in the projects
msedgedriver from here: https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
And finally in your automation code, when the window with WebView2 in it finally appears, you need to run this code to tie selenium webdriver to the WebView2 pane:
Dim driver As String = "[path to:]msedgedriver.exe"
Dim edgeOptions As New EdgeOptions
edgeOptions.UseChromium = True
edgeOptions.UseWebView = True
edgeOptions.DebuggerAddress = "localhost:9222"
Dim WebDriver = New EdgeDriver(EdgeDriverService.CreateDefaultService(".", driver), edgeOptions)

Related

Web UI methods don't work correctly when I use Selenium web driver in Katalon Studio

Good Morning,
I'm using Katalon studio to automate my testing, and I need to launch the chrome driver using selenium, then use it to call Web UI methods in the test cases. the driver launched and opened successfully, but the Web UI methods such as (click, send keys…) in the test cases marked as passed while actually they didn’t, therefore the next step can’t be executed.
it’s noteworthy that the same method sometimes works and sometimes doesn’t.
My Code
def public downloadDocumentSetup() {
String OldDownloadsPath=RunConfiguration. getProjectDir () + "/Downloads"
String DownloadsPath= OldDownloadsPath.replace("/", "\\")
HashMap<Object, String> chromePrefs = new HashMap<Object, String>();
chromePrefs.put("download.default_directory", DownloadsPath)
chromePrefs.put("profile.default_content_settings.popups",0)
chromePrefs.put("download.prompt_for_download", false)
chromePrefs.put("safebrowsing.enabled", "true");
ChromeOptions options = new ChromeOptions()
options.setExperimentalOption("prefs", chromePrefs)
DesiredCapabilities cap = DesiredCapabilities.chrome()
cap.setCapability(ChromeOptions.CAPABILITY, options)
System.setProperty("webdriver.chrome.driver", DriverFactory.getChromeDriverPath())
WebDriver driver = new ChromeDriver(cap)
DriverFactory.changeWebDriver(driver)
RunConfiguration. setWebDriverPreferencesProperty ("prefs", chromePrefs)
}
My Test Case
WebUI.navigateToUrl(GlobalVariable.WWWSite)
WebUI.click(findTestObject(‘Pages/MyFirstObject’))
WebUI.click(findTestObject(‘Pages/MySecondObject’))
Result
The browser opened and navigated to the website successfully, and the first click step marked as passed but actually it didn’t pass, therefore the next click action is not working.
Note
My Test case works perfectly when I use ‘Open Browser’ method to launch the driver.

Using Selenium and VBA to navigate Chrome, but getting popup: Loading of unpacked extensions is disabled by the administrator

I am trying to use selenium to open Chrome, but I am getting an popup that I can't resolve. It seems a solution has been found using JAVA, but I can't easily find something for VBA. In case the image doesn't load, it says "Failed to load extension from: <>local location<>. Loading of unpacked extensions is disabled by the administrator."
The closest thing I have found is this line, but it doesn't seem to do anything.
.SetPreference "profile.default_content_setting_values.notifications", 2
Here is the full code:
Option Explicit
Public Sub Demo()
Dim d As WebDriver
Set d = New ChromeDriver
Const URL = "https://www.google.com/"
With d
.SetPreference "profile.default_content_setting_values.notifications", 2
.Start "Chrome"
.Get URL
.FindElementById("lst-ib").SendKeys "Selenium basic GitHub"
.FindElementsByTag("form")(1).FindElementByCss("input[value='Google Search']").Click
'.Quit
End With
End Sub

How to enable IE mode in Chromium Edge Browser in selenium C#?

I want to automate a website in Edge which is require IE mode to be enabled. How can launch Edge in IE mode in selenium?
Below code which I currently use launches Edge in non IE mode, which won't display the website properly.
Dim edgeDriverService = Microsoft.Edge.SeleniumTools.EdgeDriverService.CreateChromiumService()
Dim edgeOptions = New Microsoft.Edge.SeleniumTools.EdgeOptions()
edgeOptions.PageLoadStrategy = PageLoadStrategy.Normal
edgeOptions.UseChromium = True
Dim driver As IWebDriver = New Microsoft.Edge.SeleniumTools.EdgeDriver(edgeDriverService, edgeOptions)
driver.Navigate().GoToUrl("http://example.com")
Tried using edgeOptions.AddAdditionalCapability("ie.edgechromium", True)but it didn't work
You could refer to the section Automating Internet Explorer mode in this article about how to use IE mode in Edge Chromium in Selenium C#.
You could refer to the following steps:
Download the latest version of IEDriverServer from Selenium site. Here I use 32 bit Windows IE version 3.150.1.
Make some preparations to use IEDriver according to this.
Create a C# console project using Visual Studio.
Install Selenium.WebDriver 3.141.0 nuget package from Nuget package manager.
Add the code below to the project and modify the paths to your owns in the code:
static void Main(string[] args)
{
var dir = "{FULL_PATH_TO_IEDRIVERSERVER}";
var driver = "IEDriverServer.exe";
if (!Directory.Exists(dir) || !File.Exists(Path.Combine(dir, driver)))
{
Console.WriteLine("Failed to find {0} in {1} folder.", dir, driver);
return;
}
var ieService = InternetExplorerDriverService.CreateDefaultService(dir, driver);
var ieOptions = new InternetExplorerOptions{};
ieOptions.AddAdditionalCapability("ie.edgechromium", true);
ieOptions.AddAdditionalCapability("ie.edgepath", "{FULL_PATH_TO_MSEDGE.EXE}");
var webdriver = new InternetExplorerDriver(ieService, ieOptions, TimeSpan.FromSeconds(30));
webdriver.Url = "http://www.example.com";
}
Run the project to test:
Notes:
Make sure to close all the Edge browser tabs and window before running the code.
Use full paths in the code. For example: ieOptions.AddAdditionalCapability("ie.edgepath", #"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe");.

Windows Internet Explorer Modal dialog box preventing selenium test run

I am trying to execute test cases in IE 8. But sometimes I am getting Windows Internet Explorer Security Warning "Do you want to enable the security content for this page" or Modal dialog box "Stop running this script...". How do I handle it in selenium Webdriver. Any help will be greatly appreciated.
There is an option in InternetExplorerOptions which gives you the opportunity to automatically close the modal if unexpected.
This is how I handle in C# and this should be fairly similar in Java or other bindings as well.
var options = new InternetExplorerOptions { EnableNativeEvents = false };
options.EnsureCleanSession = true;
//This is the main hook
options.AddAdditionalCapability("disable-popup-blocking", true);
Driver = new InternetExplorerDriver(options);

How to set the PhantomJSOptions in Selenium for proxy-type?

Using Selenium and the web driver to drive phantomjs as part of a web scraping project. From the "Known Issues" for Windows at PhantomJS, it is suggested that the proxy-type be set to "none" to speed up network performance.
I tried the following:
PhantomJSOptions options = new PhantomJSOptions();
options.AddAdditionalCapability("proxy", "{proxyType:none}");
However, this sets proxy to a string and I think I need a json object. Can someone show me how to do this correctly?
Use the driver service to set options:
var phantomJSDriverService = PhantomJSDriverService.CreateDefaultService(phantomJsDir);
phantomJSDriverService.ProxyType = "none";
var driver = new PhantomJSDriver(phantomJSDriverService);