Selenium with C# 59 - File downloading and verifying in Chrome & Firefox without using AutoIt
14.4 هزار بار بازدید -
4 سال پیش
-
Agenda
What is ChromeOptions class
Agenda
What is ChromeOptions class and AddUserProfilePreference() method?
What is FirefoxOptions class and SetPreference() method?
What is File class and Exists() method?
What are FileInfo class and Name, FullName and Length properties?
ChromeOptions class and AddUserProfilePreference() method
ChromeOptions Class is used to manage options specific to chrome driver
This class is available in OpenQA.Selenium.Chrome namespace
public void AddUserProfilePreference (string preferenceName, object preferenceValue);
Adds a preference for the user-specific profile or "user data directory." If the specified preference already exists, it will be overwritten.
Code :
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.AddUserProfilePreference("download.default_directory", @"C:\Downloads");
FirefoxOptions class and SetPreference() method
FirefoxOptions Class is used to manage options specific to firefox driver
This class is available in OpenQA.Selenium.Firefox namespace
public void SetPreference (string preferenceName, string preferenceValue);
Sets a preference in the profile used by Firefox
It is a overloaded method, preferenceValue parameter data type can be bool, int, long, double and string.
Code :
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.SetPreference("browser.download.folderList", 2);
firefoxOptions.SetPreference("browser.download.dir", @"C:\Downloads");
firefoxOptions.SetPreference("browser.helperApps.neverAsk.saveToDisk", "image/png");
File class and Exists() method
File is a static class
It has methods for the creation, copying, deletion, moving, and opening of a file
public static bool Exists(string path);
Exists() method returns true if the file exists in the provided path else returns false.
It takes string(File Path) as a parameter
Code :
File.Exists(expectedFilePath);
FileInfo class and Name, FullName and Length properties
FileInfo is a sealed class
It has methods and properties for the creation, copying, deletion, moving, and opening of a file
public long Length { get; }
Gets the size, in bytes, of the current file.
public override string Name { get; }
Gets the name of the file. Code :
public virtual string FullName { get; }
Gets the full path of the file(Includes drive and folder name).
Possible Interview Questions
What is a ChromeOptions class?
What is AddUserProfilePreference() method?
What is a FirefoxOptions class?
What is SetPreference() method?
How to download and verify a file in selenium?
Code :
[TestMethod]
public void VerifyFileDownload()
{
string expectedFilePath = @"C:\Downloads\images.png";
bool fileExists = false;
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.AddUserProfilePreference("download.default_directory", @"C:\Downloads");
var driver = new ChromeDriver(chromeOptions);
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.SetPreference("browser.download.folderList", 2);
firefoxOptions.SetPreference("browser.download.dir", @"C:\Downloads");
firefoxOptions.SetPreference("browser.helperApps.neverAsk.saveToDisk", "image/png");
// mimetype
// "application/msword, application/binary, application/ris, text/csv, image/png, application/pdf, text/html, text/plain, application/zip, application/x-zip, application/x-zip-compressed, application/download, application/octet-stream "
//var driver = new FirefoxDriver(firefoxOptions);
driver.Manage().Window.Maximize();
driver.Url = "http://uitestpractice.com/Students/Widgets";
driver.FindElement(By.XPath("//button/a")).Click();
try
{
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(60));
wait.Until greater than bool lessthan (x =greaterthan fileExists = File.Exists(expectedFilePath));
Console.WriteLine("File exists : " + fileExists);
FileInfo fileInfo = new FileInfo(expectedFilePath);
Console.WriteLine("File Length : "+fileInfo.Length);
Console.WriteLine("File Name : "+fileInfo.Name);
Console.WriteLine("File Full Name :"+fileInfo.FullName);
Assert.AreEqual(1517, fileInfo.Length);
Assert.AreEqual(fileInfo.Name, "images.png");
Assert.AreEqual(fileInfo.FullName, @"C:\Downloads\images.png");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
if (File.Exists(expectedFilePath))
File.Delete(expectedFilePath);
}
driver.Quit();
}
4 سال پیش
در تاریخ 1399/02/03 منتشر شده
است.
14,474
بـار بازدید شده