Change download location and name for each file - c#

I'm doing an automation using Selenium with Chrome WebDriver. The application has to do a series of downloads that need to be saved with different names (Data + Report type) and folders that match the type of report I'm downloading.
The problem is that I can only set the default directory when I instantiate a new driver
var chromeOptions = new ChromeOptions();
chromeOptions.AddUserProfilePreference("download.default_directory", downloadDirectory);
chromeOptions.AddUserProfilePreference("intl.accept_languages", "nl");
chromeOptions.AddUserProfilePreference("disable-popup-blocking", "true");
IWebDriver driver = new ChromeDriver(#"location chromeDriver", chromeOptions);
driver.Navigate().GoToUrl(url);
Therefore, I can not rename the file name or select the corresponding directory.
Does anyone have any idea how I can do this?

You can use MS UI Automation with TestStack.White. It's quite difficult but it works for sure.
using System.Text.RegularExpressions;
using System.Windows.Automation;
using TestStack.White.InputDevices;
using TestStack.White.UIItems;
using TestStack.White.UIItems.Finders;
using TestStack.White.UIItems.WindowItems;
...
public class SaveAsWindow
{
AutomationElement _dialog;
Window _win;
public SaveAsWindow(string title)
{
List<Window> winList = TestStack.White.Desktop.Instance.Windows();
foreach (Window win in winList)
{
Regex r = new Regex(title, RegexOptions.IgnoreCase);
Match m = r.Match(win.Title);
if (m.Success)
{
_win = win;
}
}
_dialog = _win.GetElement(SearchCriteria.ByControlType(ControlType.Window));
}
public void Close()
{
Condition condition = new PropertyCondition(AutomationElement.NameProperty, "Cancel");
AutomationElement noButton = _dialog.FindFirst(TreeScope.Children, condition);
System.Windows.Point p = noButton.GetClickablePoint();
Mouse.Instance.Click(p);
}
public string FileName
{
set
{
TextBox fileName =_win.Get<TextBox>(SearchCriteria.ByAutomationId("1001"));
fileName.Text = value;
}
}
public void Save()
{
Condition condition = new PropertyCondition(AutomationElement.AutomationIdProperty, "1");
AutomationElement saveButton = _dialog.FindFirst(TreeScope.Children, condition);
System.Windows.Point p = saveButton.GetClickablePoint();
Mouse.Instance.Click(p);
System.Threading.Thread.Sleep(1000);
}
}
//// Usage
IWebDriver driver = new ChromeDriver(#"location chromeDriver", chromeOptions);
driver.Navigate().GoToUrl(url);
// it did something and save as window appears.
var saveWindow = new SaveAsWindow("title of Chrome browser");
saveWindow.FileName = "c:\what-ever.xlsx";
saveWindow.Save();

Related

IDevTools instance does not contain CreateDevToolsSession method

I'm trying to intercept URLs containing a substring in C# using selenium chrome webdriver 4.0.0-beta4.
This is what I found and changed a little bit:
using V89 = OpenQA.Selenium.DevTools.V89;
using V89Net = OpenQA.Selenium.DevTools.V89.Network;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.DevTools;
ChromeOptions options = new ChromeOptions();
ChromeDriver webDriver;
IDevTools devTools;
public void InterceptRequestWithFetch(string url)
{
options.BinaryLocation = #"C:\Program Files\Google\Chrome Beta\Application\chrome.exe";
var service = ChromeDriverService.CreateDefaultService();
service.LogPath = AppDomain.CurrentDomain.BaseDirectory + "chromedriver.log";
service.EnableVerboseLogging = true;
webDriver = new ChromeDriver(service, options);
devTools = webDriver as IDevTools;
var devToolsSession = devTools.CreateDevToolsSession();
var fetch = devToolsSession.GetVersionSpecificDomains<V89.DevToolsSessionDomains>().Fetch;
var enableCommandSettings = new V89.Fetch.EnableCommandSettings();
var requestPattern = new V89.Fetch.RequestPattern();
requestPattern.RequestStage = V89.Fetch.RequestStage.Response;
requestPattern.ResourceType = V89Net.ResourceType.XHR;
requestPattern.UrlPattern = "*://*/*.jpg*";
enableCommandSettings.Patterns = new V89.Fetch.RequestPattern[] { requestPattern };
fetch.Enable(enableCommandSettings);
fetch.RequestPaused += RequestIntercepted;
webDriver.Navigate().GoToUrl(url);
}
void RequestIntercepted(object sender, V89.Fetch.RequestPausedEventArgs e)
{
richTextBox1.AppendText(e.Request.Url);
webDriver.Quit();
}
The problem is CreateDevToolsSession() does not exists and it seems like GetDevToolsSession() is the only option which does totally different job, but I tried it anyway and then my form froze, and codes past that line never executed.
I searched last three days for a solution but its just CreateDevToolsSession(). How can I use the DevTools if I won't be able to create a session?
This worked for me. Might not be exactly what you want, but it sets up devtools and can do whatever you normally would.
using OpenQA.Selenium.DevTools;
using OpenQA.Selenium.DevTools.V96.Network;
using DevToolsSessionDomains = OpenQA.Selenium.DevTools.V96.DevToolsSessionDomains;
public void DevtoolsExample()
{
IDevToolsSession session;
DevToolsSessionDomains devToolsSession;
//Setup WebDriver and devtools
driver = new ChromeDriver();
var baseUrl = ConfigurationHelper.Get<string>("TargetUrl");
//*this appears to create devtools session or get existing
IDevTools devTools = driver as IDevTools;
session = devTools.GetDevToolsSession();
devToolsSession = session.GetVersionSpecificDomains<DevToolsSessionDomains>();
devToolsSession.Network.Enable(new EnableCommandSettings());
devToolsSession.Network.SetBlockedURLs(new SetBlockedURLsCommandSettings()
{
Urls = new string[] { "*://*/*.css", "*://*/*.jpg", "*://*/*.png" }
//Urls = new string[] { }
});
driver.Navigate().GoToUrl("https://someUrl.com");
}

Starting a specific Firefox Profile with Selenium 3

I am trying to upgrade from Selenium 2 to Selenium 3 but the old handling, which was pretty easy and fast doesn't work anymore (and the documentation is nonexisting as it seems)
This is the program at the moment and what I want is to open a Firefox driver with the profile: SELENIUM
Sadly it doesn't work and always shuts down with the Error:
An unhandled exception of type 'System.InvalidOperationException' > occurred in WebDriver.dll
Additional information: corrupt deflate stream
This is my program at the moment:
public Program()
{
FirefoxOptions _options = new FirefoxOptions();
FirefoxProfileManager _profileIni = new FirefoxProfileManager();
FirefoxDriverService _service = FirefoxDriverService.CreateDefaultService(#"C:\Programme\IMaT\Output\Release\Bin");
_service.FirefoxBinaryPath = #"C:\Program Files (x86)\Mozilla Firefox\firefox.exe";
try
{
if ((_options.Profile = _profileIni.GetProfile("SELENIUM")) == null)
{
Console.WriteLine("SELENIUM PROFILE NOT FOUND");
_profile.SetPreference("network.proxy.type", 0); // disable proxy
_profile = new FirefoxProfile();
}
}
catch
{
throw new Exception("Firefox needs a Profile with \"SELENIUM\"");
}
IWebDriver driver = new FirefoxDriver(_service,_options,new System.TimeSpan(0,0,30));
driver.Navigate().GoToUrl("ld-hybrid.fronius.com");
Console.Write("rtest");
}
static void Main(string[] args)
{
new Program();
}
Without Loading the Profile it works with just new FirefoxDriver(_service) but the profile is mandatory.
In Selenium 2 I handled it with this code:
FirefoxProfileManager _profileIni = new FirefoxProfileManager();
// use custom temporary profile
try {
if ((_profile = _profileIni.GetProfile("SELENIUM")) == null)
{
Console.WriteLine("SELENIUM PROFILE NOT FOUND");
_profile.SetPreference("network.proxy.type", 0); // disable proxy
_profile = new FirefoxProfile();
}
}
catch
{
throw new Exception("Firefox needs a Profile with \"SELENIUM\"");
}
_profile.SetPreference("intl.accept_languages", _languageConfig);
_driver = new FirefoxDriver(_profile);
Fast and simple, but as the Driver doesn't support a Constructor with service and profile I don't really know how to get this to work, any help would be appreciated
This exception is due to a bug in the .Net library. The code generating the Zip of the profile is failing to provide a proper Zip.
One way to overcome this issue would be to overload FirefoxOptions and use the archiver from .Net framework (System.IO.Compression.ZipArchive) instead of the faulty ZipStorer:
var options = new FirefoxOptionsEx();
options.Profile = #"C:\Users\...\AppData\Roaming\Mozilla\Firefox\Profiles\ez3krw80.Selenium";
options.SetPreference("network.proxy.type", 0);
var service = FirefoxDriverService.CreateDefaultService(#"C:\downloads", "geckodriver.exe");
var driver = new FirefoxDriver(service, options, TimeSpan.FromMinutes(1));
class FirefoxOptionsEx : FirefoxOptions {
public new string Profile { get; set; }
public override ICapabilities ToCapabilities() {
var capabilities = (DesiredCapabilities)base.ToCapabilities();
var options = (IDictionary)capabilities.GetCapability("moz:firefoxOptions");
var mstream = new MemoryStream();
using (var archive = new ZipArchive(mstream, ZipArchiveMode.Create, true)) {
foreach (string file in Directory.EnumerateFiles(Profile, "*", SearchOption.AllDirectories)) {
string name = file.Substring(Profile.Length + 1).Replace('\\', '/');
if (name != "parent.lock") {
using (Stream src = File.OpenRead(file), dest = archive.CreateEntry(name).Open())
src.CopyTo(dest);
}
}
}
options["profile"] = Convert.ToBase64String(mstream.GetBuffer(), 0, (int)mstream.Length);
return capabilities;
}
}
And to get the directory for a profile by name:
var manager = new FirefoxProfileManager();
var profiles = (Dictionary<string, string>)manager.GetType()
.GetField("profiles", BindingFlags.Instance | BindingFlags.NonPublic)
.GetValue(manager);
string directory;
if (profiles.TryGetValue("Selenium", out directory))
options.Profile = directory;

Reading out Edge Browser Title & Url with System.Windows.Automation

I'm trying to read out the TITLE & URL from the Microsoft EDGE Browser.
Doing this with System.Windows.Automation most preferably since the code base already uses this for other problems.
Is it possible with System.Windows.Automation?
How to access the URL?
I'm currently this far:
AutomationId "TitleBar"
ClassName "ApplicationFrameWindow"
Name = [string]
=> Reading out this element gives me the TITLE
=> Walking it's children, I find the item "addressEditBox":
AutomationId "addressEditBox"
ClassName "RichEditBox"
Name "Search or enter web address"
=> I always get back the string "Search or enter web address"
=> This is the control where the url is in, though it isn't updated as the user goes to a website, it always returns a fixed string.
In code:
var digger1 = AutomationElement.FromHandle(process.MainWindowHandle).RootElement.FindAll(TreeScope.Children, Condition.TrueCondition);
foreach(AutomationElement d1 in digger1 {
if(d1.Current.ClassName.Equals("ApplicationFrameWindow")) {
var digger2 = d1.FindAll(TreeScope.Children, Condition.TrueCondition);
foreach(AutomationElement d2 in digger2) {
if(d2.Current.ClassName.Equals("Windows.Ui.Core.CoreWindow")) {
var digger3 = d2.FindAll(TreeScope.Children, Condition.TrueCondition);
foreach(AutomationElement d3 in digger3) {
if(d3.Current.AutomationId.Equals("addressEditBox")) {
var url = d3.Current.Name;
return url;
}
}
}
}
}
}
You're almost there. You just need to get the TextPattern from the addressEditBox element. Here is a full sample Console app that dumps out all currently running Edge's windows on the desktop:
class Program
{
static void Main(string[] args)
{
AutomationElement main = AutomationElement.FromHandle(GetDesktopWindow());
foreach(AutomationElement child in main.FindAll(TreeScope.Children, PropertyCondition.TrueCondition))
{
AutomationElement window = GetEdgeCommandsWindow(child);
if (window == null) // not edge
continue;
Console.WriteLine("title:" + GetEdgeTitle(child));
Console.WriteLine("url:" + GetEdgeUrl(window));
Console.WriteLine();
}
}
public static AutomationElement GetEdgeCommandsWindow(AutomationElement edgeWindow)
{
return edgeWindow.FindFirst(TreeScope.Children, new AndCondition(
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window),
new PropertyCondition(AutomationElement.NameProperty, "Microsoft Edge")));
}
public static string GetEdgeUrl(AutomationElement edgeCommandsWindow)
{
var adressEditBox = edgeCommandsWindow.FindFirst(TreeScope.Children,
new PropertyCondition(AutomationElement.AutomationIdProperty, "addressEditBox"));
return ((TextPattern)adressEditBox.GetCurrentPattern(TextPattern.Pattern)).DocumentRange.GetText(int.MaxValue);
}
public static string GetEdgeTitle(AutomationElement edgeWindow)
{
var adressEditBox = edgeWindow.FindFirst(TreeScope.Children,
new PropertyCondition(AutomationElement.AutomationIdProperty, "TitleBar"));
return adressEditBox.Current.Name;
}
[DllImport("user32")]
public static extern IntPtr GetDesktopWindow();
}

Selenium webdriver window handles c# switchTo failed

Here comes 2 windows pop out during the testing.
my code:
string BaseWindow = driver.CurrentWindowHandle;
ReadOnlyCollection<string> handles = driver.WindowHandles;
foreach(string handle in handles)
{
Boolean a = driver.SwitchTo().Window(handle).Url.Contains("Main");
if (a == true)
{
InitialSetting.driver.SwitchTo().Window(handle);
break;
}
}
I want to switch to the window which url contains "Main". But when the test is running, it switches between two windows continuously and it doesn't stop.
I debug and found the foreach didn't break even when the boolean a is true.
How can I resolve this?
//switch to new window
driver.FindElement(By.Id("link")).Click();
//wait for new window to open
Thread.Sleep(2000);
//get the current window handles
string popupHandle = string.Empty;
ReadOnlyCollection<string> windowHandles = driver.WindowHandles;
foreach (string handle in windowHandles)
{
if (handle != existingWindowHandle)
{
popupHandle = handle; break;
}
}
//switch to new window
driver.SwitchTo().Window(popupHandle);
//check for element on new page
webElement = driver.FindElement(By.Id("four04msg"));
if(webElement.Text == "THE CONTENT YOU REQUESTED COULDN’T BE FOUND...")
{
return false;
}
else
{
return true;
}
//close the new window to navigate to the previous one
driver.close();
//switch back to original window
driver.SwitchTo().Window(existingWindowHandle);
Using the original post code.
string existingWindowHandle = driver.CurrentWindowHandle;
Its the first window.
One important thing is:
ReadOnlyCollection<string> windowHandles = driver.WindowHandles
Contains the string name object, not the Windows Title Name, for
example Collection windowHandles could contains:
Not Windows Title Name as {Menu},{PopUp}
It contains: {45e615b3-266f-4ae0-a508-e901f42a36d3},{c6010037-0be6-4842-8d38-7f37c2621e81}
IWebDriver popup = null;
string mainWindow = driver.CurrentWindowHandle;
bool foundPopupTitle = false;
foreach (string handle in driver.WindowHandles)
{
popup = driver.SwitchTo().Window(handle);
if (popup.Title.Contains(title))
{
foundPopupTitle = true;
break;
}
}
if (foundPopupTitle)
{
popup.Close();
}
//switch back to original window
driver.SwitchTo().Window(mainWindow);
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
js.ExecuteScript("window.open()");
String ventanaPrincipal = driver.CurrentWindowHandle;
List<string> listWindow = new List<string>(driver.WindowHandles);
driver.SwitchTo().Window(listWindow[1]);
driver.Navigate().GoToUrl("http:www.google.com");
IWebElement search = driver.FindElement(By.Name("q"));
search.SendKeys("RPA");
string NewWindowHandle = string.Empty;
ReadOnlyCollection<string> windowHandles = driver.WindowHandles;
NewWindowHandle = windowHandles[windowHandles.Count - 1];
driver.SwitchTo().Window(NewWindowHandle);

How can I get URLs of open pages from Chrome and Firefox?

I'm writing a system tray app that needs to check if an internal web based app is open.
I can check IE using the following:
SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows();
string filename;
bool sdOpen = false;
foreach (SHDocVw.InternetExplorer ie in shellWindows)
{
filename = Path.GetFileNameWithoutExtension(ie.FullName).ToLower();
if (filename.Equals("iexplore"))
{
string[] urlParts = (ie.LocationURL.ToString()).Split('/');
string website = urlParts[2];
if (website == "myApp:8080") { sdOpen = true; };
}
}
if (sdOpen) { Console.WriteLine("App is open"); } else { Console.WriteLine("App is not open"); };
Console.ReadKey(true);
However, some of the users using the system prefer Chrome or Firefox.
How can I do the same as above (i.e. get the urls of any open tabs in the browser) for Chrome and Firefox? (I'm not going to bother with other browsers as these are the only ones in use in our organisation.)
It's specific for every browser. That's for the major ones:
Internet Explorer - You can use SHDocVw (like you did)
Firefox - You can get the URL using DDE (source below)
Chrome - You can get the URL while enumerating all the child windows untill you get to the control with class "Chrome_OmniboxView" and then get the text using GetWindowText
Opera - You can use the same thing as Firefox, but with "opera"
Safari - There is no known method since it uses custom drawn controls
EDIT: Since 2014, Chrome has changed and you need to get the URL with Acessibility.
Code to get the URL from Firefox/Opera using DDE (which used NDDE - the only good DDE wrapper for .NET):
//
// usage: GetBrowserURL("opera") or GetBrowserURL("firefox")
//
private string GetBrowserURL(string browser) {
try {
DdeClient dde = new DdeClient(browser, "WWW_GetWindowInfo");
dde.Connect();
string url = dde.Request("URL", int.MaxValue);
string[] text = url.Split(new string[] { "\",\"" }, StringSplitOptions.RemoveEmptyEntries);
dde.Disconnect();
return text[0].Substring(1);
} catch {
return null;
}
}
Using UIAutomation - get urls for FireFox and Chrome:
else if (browser == BrowserType.Chrome)
{
//"Chrome_WidgetWin_1"
Process[] procsChrome = Process.GetProcessesByName("chrome");
foreach (Process chrome in procsChrome)
{
// the chrome process must have a window
if (chrome.MainWindowHandle == IntPtr.Zero)
{
continue;
}
//AutomationElement elm = AutomationElement.RootElement.FindFirst(TreeScope.Children,
// new PropertyCondition(AutomationElement.ClassNameProperty, "Chrome_WidgetWin_1"));
// find the automation element
AutomationElement elm = AutomationElement.FromHandle(chrome.MainWindowHandle);
// manually walk through the tree, searching using TreeScope.Descendants is too slow (even if it's more reliable)
AutomationElement elmUrlBar = null;
try
{
// walking path found using inspect.exe (Windows SDK) for Chrome 29.0.1547.76 m (currently the latest stable)
var elm1 = elm.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Google Chrome"));
var elm2 = TreeWalker.ControlViewWalker.GetLastChild(elm1); // I don't know a Condition for this for finding :(
var elm3 = elm2.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, ""));
var elm4 = elm3.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ToolBar));
elmUrlBar = elm4.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Address and search bar"));
}
catch
{
// Chrome has probably changed something, and above walking needs to be modified. :(
// put an assertion here or something to make sure you don't miss it
continue;
}
// make sure it's valid
if (elmUrlBar == null)
{
// it's not..
continue;
}
// elmUrlBar is now the URL bar element. we have to make sure that it's out of keyboard focus if we want to get a valid URL
if ((bool)elmUrlBar.GetCurrentPropertyValue(AutomationElement.HasKeyboardFocusProperty))
{
continue;
}
// there might not be a valid pattern to use, so we have to make sure we have one
AutomationPattern[] patterns = elmUrlBar.GetSupportedPatterns();
if (patterns.Length == 1)
{
string ret = "";
try
{
ret = ((ValuePattern)elmUrlBar.GetCurrentPattern(patterns[0])).Current.Value;
}
catch { }
if (ret != "")
{
// must match a domain name (and possibly "https://" in front)
if (Regex.IsMatch(ret, #"^(https:\/\/)?[a-zA-Z0-9\-\.]+(\.[a-zA-Z]{2,4}).*$"))
{
// prepend http:// to the url, because Chrome hides it if it's not SSL
if (!ret.StartsWith("http"))
{
ret = "http://" + ret;
}
return ret;
}
}
continue;
}
}
}
else if (browser == BrowserType.Firefox)
{
AutomationElement root = AutomationElement.RootElement.FindFirst(TreeScope.Children,
new PropertyCondition(AutomationElement.ClassNameProperty, "MozillaWindowClass"));
Condition toolBar = new AndCondition(
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ToolBar),
new PropertyCondition(AutomationElement.NameProperty, "Browser tabs"));
var tool = root.FindFirst(TreeScope.Children, toolBar);
var tool2 = TreeWalker.ControlViewWalker.GetNextSibling(tool);
var children = tool2.FindAll(TreeScope.Children, Condition.TrueCondition);
foreach (AutomationElement item in children)
{
foreach (AutomationElement i in item.FindAll(TreeScope.Children, Condition.TrueCondition))
{
foreach (AutomationElement ii in i.FindAll(TreeScope.Element, Condition.TrueCondition))
{
if (ii.Current.LocalizedControlType == "edit")
{
if (!ii.Current.BoundingRectangle.X.ToString().Contains("empty"))
{
ValuePattern activeTab = ii.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;
var activeUrl = activeTab.Current.Value;
return activeUrl;
}
}
}
}
}
}
Maybe this code can help something;
Thanks to BLEZ for share this code. I use this code to capture unique addresses from firefox and add them to a listbox. But I think this is not for Chrome right?
(you Should add NDde.dll to your project, to do this go to solution explorer right click to References-> add Reference->Browse-> find that DLL (http://ndde.codeplex.com/ from binary folder.))
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NDde.Client;
namespace WindowsFormsApplication9
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private string GetBrowserURL(string browser)
{
try
{
DdeClient dde = new DdeClient(browser, "WWW_GetWindowInfo");
dde.Connect();
string url = dde.Request("URL", int.MaxValue);
string[] text = url.Split(new string[] { "\",\"" }, StringSplitOptions.RemoveEmptyEntries);
dde.Disconnect();
return text[0].Substring(1);
}
catch
{
return null;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
int j=0;
for (int i = 0; i < listBox1.Items.Count; i++)
{
if (listBox1.Items[i].ToString() == GetBrowserURL("Firefox"))
{
break;
}
else
{
j++;
}
}
if (j == listBox1.Items.Count)
{
listBox1.Items.Add(GetBrowserURL("Firefox"));
}
}
}
}
Below code work pretty well with Chrome Version 58.0.3029.110:
Please add reference of UIAutomationClient and UIAutomationProvider from Assembly provided by .NET.
foreach (Process proc in procsChrome)
{
// the chrome process must have a window
if (proc.MainWindowHandle == IntPtr.Zero)
continue;
// to find the tabs we first need to locate something reliable - the 'New Tab' button
AutomationElement root = AutomationElement.FromHandle(proc.MainWindowHandle);
var SearchBar = root.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Address and search bar"));
if (SearchBar != null)
return (string)SearchBar.GetCurrentPropertyValue(ValuePatternIdentifiers.ValueProperty);
}

Categories