Let's Start The Test automation with SELENIUM -Part 3
Example of Scraping with Selenium WebDriver in C#
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox ;
using OpenQA.Selenium.Support.UI;
using System.Threading;
using System.Threading.Tasks;
namespace UnitTestProject3
{
//Start the Test Class
[TestClass]
public class UnitTest1
{
//Start the TestMethode
[TestMethod]
public void TestMethod1()
{
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://testing-ground.scraping.pro/login");
driver.Manage().Window.Maximize();
IWebElement userNameField = driver.FindElement(By.Id("usr"));
userNameField.Clear();
userNameField.SendKeys("admin");
driver.FindElement(By.Id("pwd")).Clear();
driver.FindElement(By.Id("pwd")).SendKeys("12345");
var loginButton = driver.FindElement(By.XPath("//input[@value='Login']"));
loginButton.Click();
Thread.Sleep(2000);
var result = driver.FindElement(By.XPath("//div[@id='case_login']/h3")).Text;
File.WriteAllText("result.txt", result);
Console.WriteLine(result);
driver.Close();
}
}
}
Output -
using System.IO;
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox ;
using OpenQA.Selenium.Support.UI;
using System.Threading;
using System.Threading.Tasks;
2.The design of program
namespace UnitTestProject3
[TestClass]
public class UnitTest1
TestMethod]
public void TestMethod1()
Under the namespace there is a Test class and Under that test class there are Test method.
3.Following piece of code searches for the Firefox.exe file. If this file is located in a directory different from the directory where our program is executed, then we need to specify explicitly its path in the FirefoxDriver constructor.
IWebDriver driver = new FirefoxDriver();
4. When an instance of FirefoxDriver is created, a new Firefox browser will be started. Now we can control this browser via the driver variable. Let’s navigate to the target URL first:
driver.Navigate().GoToUrl(http://testing-ground.scraping.pro/login);
5.Maxmies the Firefox browser window
driver.Manage().Window.Maximize();
6.Then we are going to find the web page elements needed for us to login in the private area of the website .Some are using several way to that I have explain some of them
IWebElement userNameField = driver.FindElement(By.Id("usr"));
userNameField.Clear();
userNameField.SendKeys("admin");
Using clear() I am going to clear the text filed which was found by ID locator .And other one is Sendkeys() using this one you can send any Username to given text box. I am not going to explain here what are the Locator we can use .Hope to explain next level. Enter Password also done as it is .
driver.FindElement(By.Id("pwd")).Clear();
driver.FindElement(By.Id("pwd")).SendKeys("12345");
Now we are going to use XPath to identify the Login button in web page. Then click on login button using Click() method as following .
var loginButton = driver.FindElement(By.XPath("//input[@value='Login']"));
loginButton.Click();
7.At this point the new page will be loaded into the browser, and after it’s done we can scrape the text we need and save it into the file:
var result = driver.FindElement(By.XPath("//div[@id='case_login']/h3")).Text;
File.WriteAllText("result.txt", result);
8.Then I am going to write the result in console to display
Console.WriteLine(result);
9.Now we have to close browser
driver.Close();
I will explain command which we have to use in selenium usually with C# Language. See the next Level of post
No comments:
Post a Comment