Showing posts with label csharp. Show all posts
Showing posts with label csharp. Show all posts

Tuesday, July 21, 2015

The Siplest way to set proxy settings

Firefox:
String PROXY = "localhost:8080";

org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
proxy.setHttpProxy(PROXY)
     .setFtpProxy(PROXY)
     .setSslProxy(PROXY);
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(CapabilityType.PROXY, proxy);
IWebDriver driver = new FirefoxDriver(cap);

IE & Chrome - Chrome will take same cofiguration what IE uses

String PROXY = "localhost:8080";

org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
proxy.setHttpProxy(PROXY)
     .setFtpProxy(PROXY)
     .setSslProxy(PROXY);
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(CapabilityType.PROXY, proxy);

IWebDriver driver = new InternetExplorerDriver(cap);


The Advanced User Interactions require native events to be enabled. Here’s a table of the current support Matrix for native events:
platformIE6IE7IE8IE9FF3.6FF10+Chrome stableChrome betaChrome devOperaAndroidiOS
Windows XPYYYn/aYYYYY?[1]n/a
Windows 7n/an/aYYYYYYY?[1]n/a
Linux (Ubuntu)n/an/an/an/a[2][2]YYY?[1]n/a
Mac OSXn/an/an/an/aNNYYY?[1]N
Mobile Devicen/an/an/an/an/a?n/an/an/a?YN
[1](1234) Using the emulator
[2](12) With explicitly enabling native events




Monday, July 13, 2015

Use following generic method for get child XPath

public static string FindChildXPath(string element)
        {
            string childXpath = "//[@id='" + element + "']/" + element[1] + "/";
            return childXpath;
        }
Use following method to get attribute value using css selector

public String GetIDcss(string css)
        {
            string id = driver.FindElement(By.CssSelector(@" + css + @")).GetAttribute("id");
            return id;
        }
Use following simple method to get Id attribute using XPath,

public string GetIDxpath(string xPath)
        {
            string id = driver.FindElement(By.XPath(@" + xPath + @")).GetAttribute("id");
            return id;
        }


Simple way to handle double click the button.

public void DoubleClick(IWebDriver driver, IWebElement objectName)
        {
            Actions actionProvider = new Actions(driver);
            actionProvider.DoubleClick(this.objectName);
        }

Tuesday, July 7, 2015

Try with different IE drivers. Even through IE 64 bit installed in machine mostly IE 32 bit only will run. Its better to user  following IE driver (https://code.google.com/p/selenium/downloads/list)

We can also select certain elements that have certain attribute values.
Syntax: css=Tagname[Att=Value@Att]
Ex: 
<input type="text" class="europe asia africa" value="Continent" />

driver.FindElement(By.CssSelector("input[value=Continent]"))

We can also select elements with multiple attribute values.
Syntax: css=Tagname[Att=Value@Att][Att1=Value@Att1]

<input type="text" class="europe asia africa" value="Continent" />

driver.FindElement(By.CssSelector("input[value=Continent][type=text]))
Sometimes when a particular selector matches more than one element, you may need to combine selectors to clarify things
Here is some of that syntax

Ex: <div id='party' class='btCls'>

css=div#party

driver.FindElement(By.CssSelector("div#party"))
In the above example, we are combining the div element selector with the party id selector

css=div.btCls

driver.FindElement(By.CssSelector("div.btCls"))
In the above example, we are combining the div element selector with the btCls class selector

css=div#party.btCls

driver.FindElement(By.CssSelector("div#party.btCls"))
In the above example, we are combining the div element selector with the party id selector & btCls class selector
There are several CSS selectors, but I am going to introduce you to a few of them as we gradually build our case to more complex examples.

(a)Element Selectors - Element selectors use the name of the element(tagname) to locate the element.
EX:
<input id="firstName" name="fname" type="text" value="" />

driver.FindElement(By.CSSSelector("input")); 

(b)ID Selectors - ID selectors use place a hash or pound sign in front of the id value of that element
EX:
<input id="firstName" name="fname" type="text" value="" />

driver.FindElement(By.CSSSelector("input#firstName")); 


(c)Class Selectors -Class selectors place a dot in front of the class attribute value of that element

EX:
<input id="firstName" class="btCls" type="text" value="" />

driver.FindElement(By.CSSSelector("input.btCls")); 
Here you can find the script for protractor

Add reference Protractor library to Application,

using Protractor; // Namespace need to be included

using (IWebDriver ngDriver = new NgWebDriver(new FirefoxDriver()))
{
                ngDriver.Url = "http://www.angularjs.org";

                ngDriver.FindElement(NgBy.Model("yourName")).SendKeys("Selenium");

                System.Console.WriteLine("TEST : " + ngDriver.FindElement(NgBy.Model("yourName")).Text);
                Assert.AreEqual("Hello Selenium!", ngDriver.FindElement(NgBy.Binding("yourName")).Text);
         
}
How to get rows from a table

IWebElement table = driver.FindElement(By.Id("summaryTable"));

List<IWebElement> rows = table.FindElements(By.TagName("tr"));

assertEquals(10, rows.size());

Sunday, July 5, 2015

Selenium Automation script execution frameworks :

NUnit - Nuit test runner
MBUnit - Gallio test runner
Specflow - NUnit / MS Test runner


Selenium  Automation Script Design Frameworks:

Data Driven
Keyword driven
Leaner driven
Modular driven
Page Object Model


Implicit wait will initialize once in a life time of webdriver object.
After each and every statement execution of webdriver implicit wait will be invoked.
It will wait for specified time period to execute the statement. if it executed before the time period remaining time will be ignored and continue to the next statement.

Ex:

IWebDriver driver = new FirefoxDriver();
driver.Manage().Timeouts().ImplicitlyWait(10, TimeSpan.SECONDS);

After every statement execution it will for 10 seconds, suppose if the statement executed in 4 sec. it will suppress remaining 6 sec.

Saturday, June 20, 2015

By ID  : Locates an element the using ID attribute
Ex: driver.FindElement(By.Id("Value @ Id Attribute"))

By Name : Locates an element using the Name attribute
Ex: driver.FindElement(By.Name("Value @ Name Attribute"))

By class name : Locates an element using the Class attribute
Ex: driver.FindElement(By.ClassName("Value @ Class Attribute"))

By tag name : Locates an element using the tag name
Ex: driver.FindElement(By.TagName("HTML tag name"))

By link text : Locates link using it's text
Ex: driver.FindElement(By.LinkText("Value @ element"))

By partial link text : Locates link using it's partial text
Ex: driver.FindElement(By.PartialLinkText("Value @ element"))

By CSS : Locates element using the CSS selector
Ex: driver.FindElement(By.CssSelector("css selector"))

By XPathLocates element using XPath query
Ex: driver.FindElement(By.XPath("xpath query expression"))

Thursday, June 18, 2015


Multiple windows Handling

 driver.Navigate().GoToUrl("Sample1.html");
            driver.FindElement(By.LinkText("Visit W3Schools!")).Click();

            string BaseWindow = driver.CurrentWindowHandle;

            ReadOnlyCollection<string> handles = driver.WindowHandles;

            foreach (string handle in handles)
            {
                Boolean a = driver.SwitchTo().Window(handle).Url.Contains("http://www.w3schools.com/");
                if (a == true)
                {
                    driver = driver.SwitchTo().Window(handle);
                    break;
                }
            }

            driver.FindElement(By.LinkText("LEARN HTML")).Click();


            driver = driver.SwitchTo().Window(BaseWindow);


sample HTML

<!DOCTYPE html>
<html>
<body>

<iframe width="100%" height="300px" src="///F:/Yash/Selenium_Workshop_Training/htmls/Alerts.html" name="iframe_a"></iframe>
<p><a href="file:///F:/Yash/Selenium_Workshop_Training/htmls/Alerts.html" target="iframe_a">Selenium_Workshop_Training</a></p>

<p>When the target of a link matches the name of an iframe, the link will open in the iframe.</p>
<a href="http://www.w3schools.com/" target="_blank">Visit W3Schools!</a>
</body>
</html>


driver.Navigate().GoToUrl("http://www.google.com");
            ITakesScreenshot screenshotDriver = driver as ITakesScreenshot;
            Screenshot screenshot = screenshotDriver.GetScreenshot();
            screenshot.SaveAsFile(@"F:\Selenium_Workshop\test.jpeg", ImageFormat.Jpeg);

driver.Navigate().GoToUrl("http://www.google.com");

            var title = ((IJavaScriptExecutor)driver).ExecuteScript("return document.title");

            System.Console.WriteLine("Title of the window : " + title);

            var js = (IJavaScriptExecutor)driver;

            var length = js.ExecuteScript("var links = document.getElementsByTagName('A'); return links.length");

            System.Console.WriteLine("Length of Element : " + length);
public class Factory
    {
        public static IWebDriver Driver;

        public static void CreateDriverInstance(string browserType)
        {
            if (browserType.Equals("Firefox"))
            {
                Driver = new FirefoxDriver();
            }
            else if (browserType.Equals("IE"))
            {
                Driver = new InternetExplorerDriver();
            }
            else if (browserType.Equals("Chrome"))
            {
                Driver = new ChromeDriver();
            }

            Driver.Manage().Window.Maximize();
            Driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));

            Driver.Navigate().GoToUrl("http://www.google.com");
        }