How to get browser name, version and operating system detail in selenium 2(Web driver)
2013-08-30 13:25
726 查看
C#:
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.UI;
using System.Web;
using System.Configuration;
using System.IO;
if (!Directory.Exists(OutPutFilePath))
Directory.CreateDirectory(OutPutFilePath);
var fileName = "TestMediaForgeAdsResult_" + DateTime.Now.ToString("yyyyMMddHHmmss") + "." + OutPutFileType;
var filePath = Path.Combine(OutPutFilePath, fileName);
if (!File.Exists(filePath))
{
var fs = File.Create(filePath);
fs.Close();
}
using (StreamWriter sw = new StreamWriter(filePath))
{
Console.WriteLine("Please wait for the content is showing ......");
Console.WriteLine("Visiting our ads page: " + OurAdsHostPage);
Console.WriteLine("---------------------------------------------------------------");
sw.WriteLine("Visiting our ads page: " + OurAdsHostPage);
sw.WriteLine("---------------------------------------------------------------");
//Console.WriteLine(OutPutFileType);
//Console.WriteLine(OutPutFilePath);
try
{
_driver = new FirefoxDriver();
_driver.Navigate().GoToUrl(OurAdsHostPage);
//var outputHtml = HttpUtility.HtmlDecode(_driver.PageSource);
//Console.WriteLine("view output content-----");
//Console.WriteLine(outputHtml);
ReadOnlyCollection<IWebElement> elements = _driver.FindElements(By.TagName("iframe"));
var m = 1;
var adUrl = string.Empty;
for (var i = 0; i < elements.Count(); i++)
{
adUrl = elements[i].GetAttribute("src");
if (adUrl.StartsWith("http://ads."))
{
_subDriver = new FirefoxDriver();
Console.WriteLine("URL for Ad-{0}:", m);
sw.WriteLine("URL for Ad-{0}:", m);
Console.WriteLine(adUrl);
sw.WriteLine(adUrl);
Console.WriteLine("Visiting the Ad-{0} URL ......", m);
sw.WriteLine("Visiting the Ad-{0} URL ......", m);
System.Threading.Thread.Sleep(3000);
_subDriver.Navigate().GoToUrl(adUrl);
//Console.WriteLine("Outputting the response of visiting Ad-{0} ......", m);
//Console.WriteLine(_subDriver.PageSource);
Console.WriteLine("Response status: 200 OK");
Console.WriteLine("Response title: " + _subDriver.Title);
Console.WriteLine("Response time: " + DateTime.Now.ToLongDateString());
sw.WriteLine("Response status: 200 OK");
sw.WriteLine("Response title: " + _subDriver.Title);
sw.WriteLine("Response time: " + DateTime.Now.ToLongDateString());
var caps = ((RemoteWebDriver) _subDriver).Capabilities;
var browserName = caps.BrowserName;
var browserVersion = caps.Version;
var platForm = caps.Platform;
Console.WriteLine("Browser Name: " + browserName);
Console.WriteLine("Browser Version: " + browserVersion);
Console.WriteLine("Plat form: " + platForm);
sw.WriteLine("Browser Name: " + browserName);
sw.WriteLine("Browser Version: " + browserVersion);
sw.WriteLine("Plat form: " + platForm);
for (var j = 0; j < _subDriver.Manage().Cookies.AllCookies.Count(); j++)
{
Console.WriteLine("Cookies[{0}] information:", j);
Console.WriteLine(" Name: " + _subDriver.Manage().Cookies.AllCookies[0].Name);
Console.WriteLine(" Domain: " + _subDriver.Manage().Cookies.AllCookies[0].Domain);
Console.WriteLine(" Value: " + _subDriver.Manage().Cookies.AllCookies[0].Value);
var cookieExpiry = _subDriver.Manage().Cookies.AllCookies[j].Expiry;
Console.WriteLine(" Expiry: " + (cookieExpiry == null ? "null" : cookieExpiry.ToString()));
Console.WriteLine("");
sw.WriteLine("Cookies[{0}] information:", j);
sw.WriteLine(" Name: " + _subDriver.Manage().Cookies.AllCookies[0].Name);
sw.WriteLine(" Domain: " + _subDriver.Manage().Cookies.AllCookies[0].Domain);
sw.WriteLine(" Value: " + _subDriver.Manage().Cookies.AllCookies[0].Value);
sw.WriteLine(" Expiry: " + (cookieExpiry == null ? "null" : cookieExpiry.ToString()));
sw.WriteLine("");
}
System.Threading.Thread.Sleep(3000);
_subDriver.Quit();
m++;
Console.WriteLine("---------------------------------------------------------------");
sw.WriteLine("---------------------------------------------------------------");
}
}
}
catch (Exception ex)
{
Console.WriteLine("The error occurred: " + ex.Message);
sw.WriteLine("The error occurred: " + ex.Message);
}
finally
{
_driver.Quit();
Console.WriteLine("Please tab enter to exit!");
Console.Read();
}
}
}
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.UI;
using System.Web;
using System.Configuration;
using System.IO;
if (!Directory.Exists(OutPutFilePath))
Directory.CreateDirectory(OutPutFilePath);
var fileName = "TestMediaForgeAdsResult_" + DateTime.Now.ToString("yyyyMMddHHmmss") + "." + OutPutFileType;
var filePath = Path.Combine(OutPutFilePath, fileName);
if (!File.Exists(filePath))
{
var fs = File.Create(filePath);
fs.Close();
}
using (StreamWriter sw = new StreamWriter(filePath))
{
Console.WriteLine("Please wait for the content is showing ......");
Console.WriteLine("Visiting our ads page: " + OurAdsHostPage);
Console.WriteLine("---------------------------------------------------------------");
sw.WriteLine("Visiting our ads page: " + OurAdsHostPage);
sw.WriteLine("---------------------------------------------------------------");
//Console.WriteLine(OutPutFileType);
//Console.WriteLine(OutPutFilePath);
try
{
_driver = new FirefoxDriver();
_driver.Navigate().GoToUrl(OurAdsHostPage);
//var outputHtml = HttpUtility.HtmlDecode(_driver.PageSource);
//Console.WriteLine("view output content-----");
//Console.WriteLine(outputHtml);
ReadOnlyCollection<IWebElement> elements = _driver.FindElements(By.TagName("iframe"));
var m = 1;
var adUrl = string.Empty;
for (var i = 0; i < elements.Count(); i++)
{
adUrl = elements[i].GetAttribute("src");
if (adUrl.StartsWith("http://ads."))
{
_subDriver = new FirefoxDriver();
Console.WriteLine("URL for Ad-{0}:", m);
sw.WriteLine("URL for Ad-{0}:", m);
Console.WriteLine(adUrl);
sw.WriteLine(adUrl);
Console.WriteLine("Visiting the Ad-{0} URL ......", m);
sw.WriteLine("Visiting the Ad-{0} URL ......", m);
System.Threading.Thread.Sleep(3000);
_subDriver.Navigate().GoToUrl(adUrl);
//Console.WriteLine("Outputting the response of visiting Ad-{0} ......", m);
//Console.WriteLine(_subDriver.PageSource);
Console.WriteLine("Response status: 200 OK");
Console.WriteLine("Response title: " + _subDriver.Title);
Console.WriteLine("Response time: " + DateTime.Now.ToLongDateString());
sw.WriteLine("Response status: 200 OK");
sw.WriteLine("Response title: " + _subDriver.Title);
sw.WriteLine("Response time: " + DateTime.Now.ToLongDateString());
var caps = ((RemoteWebDriver) _subDriver).Capabilities;
var browserName = caps.BrowserName;
var browserVersion = caps.Version;
var platForm = caps.Platform;
Console.WriteLine("Browser Name: " + browserName);
Console.WriteLine("Browser Version: " + browserVersion);
Console.WriteLine("Plat form: " + platForm);
sw.WriteLine("Browser Name: " + browserName);
sw.WriteLine("Browser Version: " + browserVersion);
sw.WriteLine("Plat form: " + platForm);
for (var j = 0; j < _subDriver.Manage().Cookies.AllCookies.Count(); j++)
{
Console.WriteLine("Cookies[{0}] information:", j);
Console.WriteLine(" Name: " + _subDriver.Manage().Cookies.AllCookies[0].Name);
Console.WriteLine(" Domain: " + _subDriver.Manage().Cookies.AllCookies[0].Domain);
Console.WriteLine(" Value: " + _subDriver.Manage().Cookies.AllCookies[0].Value);
var cookieExpiry = _subDriver.Manage().Cookies.AllCookies[j].Expiry;
Console.WriteLine(" Expiry: " + (cookieExpiry == null ? "null" : cookieExpiry.ToString()));
Console.WriteLine("");
sw.WriteLine("Cookies[{0}] information:", j);
sw.WriteLine(" Name: " + _subDriver.Manage().Cookies.AllCookies[0].Name);
sw.WriteLine(" Domain: " + _subDriver.Manage().Cookies.AllCookies[0].Domain);
sw.WriteLine(" Value: " + _subDriver.Manage().Cookies.AllCookies[0].Value);
sw.WriteLine(" Expiry: " + (cookieExpiry == null ? "null" : cookieExpiry.ToString()));
sw.WriteLine("");
}
System.Threading.Thread.Sleep(3000);
_subDriver.Quit();
m++;
Console.WriteLine("---------------------------------------------------------------");
sw.WriteLine("---------------------------------------------------------------");
}
}
}
catch (Exception ex)
{
Console.WriteLine("The error occurred: " + ex.Message);
sw.WriteLine("The error occurred: " + ex.Message);
}
finally
{
_driver.Quit();
Console.WriteLine("Please tab enter to exit!");
Console.Read();
}
}
}
相关文章推荐
- How to Configure Selenium Webdriver in Eclipse and execute some simple test script.
- JS: How to detect my browser version and operating system using JavaScript?
- How can selenium web driver get to know when the new window has opened and then resume its execution
- how to get url and hostname and url text in one page
- How to enable USB selective suspend and system wake in the UMDF driver for a USB device
- Unable to get browser (Selenium::WebDriver::Error::NoSuchWindowError)
- How to get Intellisense for Web.config and App.config in Visual Studio .NET?(转载)
- How to get operating system info in .NET
- How to take partial screenshot with Selenium WebDriver in python
- 在64位系统下安装32位ODBC驱动问题How to install and configure a 32 bit ODBC driver on a 64 bit Operating System?
- How to Get App Name and Version
- How can I get a FlowDocument Hyperlink to launch browser and go to URL in a WPF app?
- [笔记] How to get the versionCode and versionName of your app
- How to get and set the system socket buffer in AIX?
- How to get HTML code of a WebElement in Selenium
- How to get latest version in Windchill
- How to uninstall CUDA driver and toolkit in Mac OS X?
- How To Get System Info In Linux
- How to Get First and Last Day of a Week in SQL Server
- How to write a first class paper and get it published in a scientific journal?