Agenda:
The agenda for this blog is to Discuss various scenario based concept and code used in C# Selenium.
Highlighting Web Element in Script Run-Time:
There are some Scenarios where we need to verify/Identify WebElements in the Page ,We can do them in multiple approaches,We can High Light the Element which we are looking for and set the color with different colors for that element , in the Test Run time so we can Identify/verify that the same:
public void HighlightToVerify(By locator)
{
try{
string AttributeValue = "border:3px solid green;";
string Attribute = Driver.FindElement(locator).GetAttribute("style");
IJavaScriptExecutor JsDriver = ((IJavaScriptExecutor)Driver);
JsDriver.ExecuteScript("arguments[0].setAttribute('style', arguments[1]);", Driver.FindElement(locator), AttributeValue);
Thread.Sleep(100);
JsDriver.ExecuteScript("arguments[0].setAttribute('style', arguments[1]);", Driver.FindElement(locator), Attribute);
}
catch(Exception){
}
}
SendKeys using JavaScript:
There are scenarios where we need to use JavaScript for Entering Text in the Input field,the same can be achieved in the following way:
public void SendData(By Locator, String Text, int TimePeriod = 30)
{
try
{
IJavaScriptExecutor js = ((IJavaScriptExecutor)Driver);
js.ExecuteScript("arguments[0].innerHTML='" + Text + "';", Driver.FindElement(Locator, TimePeriod));
}
catch (Exception ex)
{
Log.Error("Unable to Enter Text using IJavaScriptExecutor for Element with Locator: "+Locator);
}
}
Presence Of Certain Text in The Page Source:
In some cases we need to check the presence of some text in the Page Source,Here we can achieve the same:
public bool IsTextPresentOnPage(String Text)
{
try
{
return Driver.PageSource.Contains(Text);
}
catch (Exception)
{
Log.Info("The Text "+ Text +"is not Present in the Page Source ");
return false;
}
}
Zoom -IN and Zoom Out :
In Selenium you might face some issues like interacting with elements that goes under task bar due to improper layout.
In this scenario WebElement will not be visible ,for this scenario you need to zoom out the page again zoom in once action is performed successfully :
class Program
{
static void Main(string[] args)
{
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("https://www.google.co.in/?gfe_rd=cr&ei=fxQ6VeiZNJTDuASzrYHQAg");
driver.Manage().Window.Maximize();
Thread.Sleep(3000);
System.Windows.Forms.SendKeys.SendWait("^{ADD}");
Thread.Sleep(3000);
System.Windows.Forms.SendKeys.SendWait("^{SUBTRACT}");
Thread.Sleep(3000);
driver.Quit();
}
}
Download or Save Files :
In selenium sometimes we need to download or save files,we can achieve it by the below approach:
static void Main(string[] args)
{
IWebDriver driver = new FirefoxDriver();
driver.Manage().Window.Maximize();
driver.Navigate().GoToUrl("http://www.seleniumhq.org/projects/webdriver/");
Thread.Sleep(3000);
System.Windows.Forms.SendKeys.SendWait("^s");
Thread.Sleep(2000);
String pathToSaveFile="C:\\Users\\Admin\\Desktop\\II";
System.Windows.Forms.SendKeys.SendWait("^a");
Thread.Sleep(2000);
System.Windows.Forms.SendKeys.SendWait("{BACKSPACE}");
Thread.Sleep(2000);
System.Windows.Forms.SendKeys.SendWait(pathToSaveFile);
Thread.Sleep(2000);
System.Windows.Forms.SendKeys.SendWait("{ENTER}");
Thread.Sleep(2000);
System.Windows.Forms.SendKeys.SendWait("{ENTER}");
Thread.Sleep(3000);
driver.Quit();
}
Visit for More Automation Related Discussion:
https://www.youtube.com/channel/UCKSk4gkmO3LXcW17hFUkmcQ/videos
The agenda for this blog is to Discuss various scenario based concept and code used in C# Selenium.
Highlighting Web Element in Script Run-Time:
There are some Scenarios where we need to verify/Identify WebElements in the Page ,We can do them in multiple approaches,We can High Light the Element which we are looking for and set the color with different colors for that element , in the Test Run time so we can Identify/verify that the same:
public void HighlightToVerify(By locator)
{
try{
string AttributeValue = "border:3px solid green;";
string Attribute = Driver.FindElement(locator).GetAttribute("style");
IJavaScriptExecutor JsDriver = ((IJavaScriptExecutor)Driver);
JsDriver.ExecuteScript("arguments[0].setAttribute('style', arguments[1]);", Driver.FindElement(locator), AttributeValue);
Thread.Sleep(100);
JsDriver.ExecuteScript("arguments[0].setAttribute('style', arguments[1]);", Driver.FindElement(locator), Attribute);
}
catch(Exception){
}
}
SendKeys using JavaScript:
There are scenarios where we need to use JavaScript for Entering Text in the Input field,the same can be achieved in the following way:
public void SendData(By Locator, String Text, int TimePeriod = 30)
{
try
{
IJavaScriptExecutor js = ((IJavaScriptExecutor)Driver);
js.ExecuteScript("arguments[0].innerHTML='" + Text + "';", Driver.FindElement(Locator, TimePeriod));
}
catch (Exception ex)
{
Log.Error("Unable to Enter Text using IJavaScriptExecutor for Element with Locator: "+Locator);
}
}
Presence Of Certain Text in The Page Source:
In some cases we need to check the presence of some text in the Page Source,Here we can achieve the same:
public bool IsTextPresentOnPage(String Text)
{
try
{
return Driver.PageSource.Contains(Text);
}
catch (Exception)
{
Log.Info("The Text "+ Text +"is not Present in the Page Source ");
return false;
}
}
Zoom -IN and Zoom Out :
In Selenium you might face some issues like interacting with elements that goes under task bar due to improper layout.
In this scenario WebElement will not be visible ,for this scenario you need to zoom out the page again zoom in once action is performed successfully :
class Program
{
static void Main(string[] args)
{
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("https://www.google.co.in/?gfe_rd=cr&ei=fxQ6VeiZNJTDuASzrYHQAg");
driver.Manage().Window.Maximize();
Thread.Sleep(3000);
System.Windows.Forms.SendKeys.SendWait("^{ADD}");
Thread.Sleep(3000);
System.Windows.Forms.SendKeys.SendWait("^{SUBTRACT}");
Thread.Sleep(3000);
driver.Quit();
}
}
Download or Save Files :
In selenium sometimes we need to download or save files,we can achieve it by the below approach:
static void Main(string[] args)
{
IWebDriver driver = new FirefoxDriver();
driver.Manage().Window.Maximize();
driver.Navigate().GoToUrl("http://www.seleniumhq.org/projects/webdriver/");
Thread.Sleep(3000);
System.Windows.Forms.SendKeys.SendWait("^s");
Thread.Sleep(2000);
String pathToSaveFile="C:\\Users\\Admin\\Desktop\\II";
System.Windows.Forms.SendKeys.SendWait("^a");
Thread.Sleep(2000);
System.Windows.Forms.SendKeys.SendWait("{BACKSPACE}");
Thread.Sleep(2000);
System.Windows.Forms.SendKeys.SendWait(pathToSaveFile);
Thread.Sleep(2000);
System.Windows.Forms.SendKeys.SendWait("{ENTER}");
Thread.Sleep(2000);
System.Windows.Forms.SendKeys.SendWait("{ENTER}");
Thread.Sleep(3000);
driver.Quit();
}
Visit for More Automation Related Discussion:
https://www.youtube.com/channel/UCKSk4gkmO3LXcW17hFUkmcQ/videos