Sunday, 23 August 2015

C# Webdriver - Fetch the names of checkboxes which are selected using 'Select' and 'Where' in a single LINQ statement.


Fetch the names of checkboxes which are selected using 'Select' and 'Where'.
using System;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium;
using System.Threading;
using System.Linq;


namespace TestConsoleApplication
{
    class Program
    {
        static void Main(string[] args)  
        {
            IWebDriver driver = new FirefoxDriver();
            driver.Manage().Window.Maximize();
            driver.Navigate().GoToUrl("http://www.tizag.com/htmlT/htmlcheckboxes.php");
            Thread.Sleep(3000);

            string xpath = "html/body/table[3]/tbody/tr[1]/td[2]/table/tbody/tr/td/div[6]/input";
            var list = driver.FindElements(By.XPath(xpath)).Where(p => p.Selected==true).Select(x => x.GetAttribute("value"));

            foreach(var text in list){
                Console.WriteLine(text);
            }
             
            Console.WriteLine("----------------");
            Console.ReadLine();

            driver.Close();
            driver.Quit();
            driver.Dispose();
        }
 
    }
}

Fetch the names of checkboxes which are selected without using 'Select' and 'Where'.
using System;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium;
using System.Threading;

namespace TestConsoleApplication
{
    class Program1
    {
        static void Main(string[] args)
        {

            IWebDriver driver = new FirefoxDriver();
            driver.Manage().Window.Maximize();
            driver.Navigate().GoToUrl("http://www.tizag.com/htmlT/htmlcheckboxes.php");
            Thread.Sleep(2000);

            string xpath = "html/body/table[3]/tbody/tr[1]/td[2]/table/tbody/tr/td/div[6]/input";
            var webElements = driver.FindElements(By.XPath(xpath));

            foreach (var webElement in webElements)
            {
                if(webElement.Selected==true){
                    Console.WriteLine(webElement.GetAttribute("value"));
                }               
            }

            Console.WriteLine("---------------");
            Console.ReadLine();

            driver.Close();
            driver.Quit();
            driver.Dispose();
        }
    }
}


Saturday, 22 August 2015

C# Webdriver - Fetch all visible URL's using 'IEnumerable' Select method.

Fetch all visible URL's using 'IEnumerable' Select method.
using System;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium;
using System.Threading;
using System.Collections.Generic;
using System.Linq;

namespace TestConsoleApplication
{
    class Program
    {
        static void Main(string[] args) 
        {
            IWebDriver driver = new FirefoxDriver();
            driver.Manage().Window.Maximize();
            driver.Navigate().GoToUrl("http://www.google.com");

            driver.FindElement(By.CssSelector("#lst-ib")).SendKeys("Webdriver");
            driver.FindElement(By.CssSelector("#lst-ib")).SendKeys(Keys.Return);
            Thread.Sleep(2000);

            var list = driver.FindElements(By.CssSelector("._Rm")).Select(x => x.Text);

            foreach(var text in list){
                Console.WriteLine(text);
            }
             
            Console.WriteLine("------------------------------------");
            Console.ReadLine();

            driver.Close();
            driver.Quit();
            driver.Dispose();
        }
 
    }
}

Fetch all visible URL's without using 'IEnumerable' Select method.
using System;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium;
using System.Threading;

namespace TestConsoleApplication
{
    class Program1
    {
        static void Main(string[] args)
        {
            IWebDriver driver = new FirefoxDriver();
            driver.Manage().Window.Maximize();
            driver.Navigate().GoToUrl("http://www.google.com");

            driver.FindElement(By.CssSelector("#lst-ib")).SendKeys("Webdriver");
            driver.FindElement(By.CssSelector("#lst-ib")).SendKeys(Keys.Return);
            Thread.Sleep(2000);

            var webElements = driver.FindElements(By.CssSelector("._Rm"));

            foreach (var webElement in webElements)
            {
                Console.WriteLine(webElement.Text);
            }

            Console.WriteLine("------------------------------------");
            Console.ReadLine();

            driver.Close();
            driver.Quit();
            driver.Dispose();
        }
    }
}


Thursday, 13 August 2015

Firepath - Inspect element by css

Prerequisite:
Firebug addon is already installed in firefox browser.
Firepath addon is already installed.

1. Navigate to the web page url,
    launch firebug addon..
2. Select 'css'

3. Click on the arrow icon to inspect the element,
    click the inspect icon(i.e. arrow icon) and move the mouse over the element to which you want to get the CSS locator. 

 

5. Re-verifying the css locator in selenium-ide (Prerequisite: Selenium IDE is already installed in firefox browser).
   launch the selenium-ide, stop the recording.
   copy the css locator and paste in selenium-ide.
   ex: .downloadBox>a

 Type 'css=' in target field.

6. click on 'Find' button in selenium-ide, element related to css locator is highlighted in the browser.

Note:
If the css locator is representing multiple elements in the browser.
Then css locator need to be modified, to uniquely identify identify the element.