Showing posts with label Selenium Webdriver. Show all posts
Showing posts with label Selenium Webdriver. Show all posts

Thursday, April 10, 2014

Talking screenshot with selenium webdriver using c# with app configuration


Sample code for talking screenshot with selenium webdriver using c# with app configuration

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support;
using OpenQA.Selenium.Support.UI;
using System;
using System.IO;

namespace ConsoleApplication5
{
    class Program
    {
        public static void Main(string[] args)
        {
            string website =                                                                     System.Configuration.ConfigurationSettings.AppSettings["url"];

            string pages =                                                                    System.Configuration.ConfigurationSettings.AppSettings["page_name"];

              string[] saparated_pages = pages.Split(',');

            //Specify browser driver path
            IWebDriver driver = new ChromeDriver(@"D:\");

            //Site Launch
            driver.Navigate().GoToUrl(website);

            //Getting screenshot
            Screenshot ss1=((ITakesScreenshot)driver).GetScreenshot();

            //Specify variable value as current time to set for filename
            var filename=DateTime.Now.ToString("MM-dd-yyyy,h-s-m tt");

            //Save the screenshot with specified location&image format
            ss1.SaveAsFile(@"D:\selenium_images\" filename+".png",
            System.Drawing.Imaging.ImageFormat.Png);

            for (int j = 0; j < 3; j++)
            {
                 driver.Navigate().GoToUrl(website+"/"+ saparated_pages[j]);
              Screenshot ss=((ITakesScreenshot)driver).GetScreenshot();
              var file_name=DateTime.Now.ToString("MM-dd-yyyy,h-s-m tt");
              ss.SaveAsFile(@"D:\selenium_images\" + file_name + ".png",                       System.Drawing.Imaging.ImageFormat.Png);
            }

            //close the browser
            driver.Close();
           
        }      
    }
}
  

//app config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="domain_name" value="http://www.google.com"/>
    <add key="page_name" value="imghp,photos,document"/>
    <add key="url" value="http://www.google.com"/>
  </appSettings>
</configuration>

To create the App Config as,

Step 1:Right the project name in solution explorer
Step 2:Add->New item
Step 3:choose Application configuration file in visual studio installed templates
Step 4:name the templates
Step 5:its open with

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
</configuration>

Step 6:include the app settings where its keys and value as above program
Step 7:to call the app config as with "System.Configuration.ConfigurationSettings.AppSettings["url"]; "
where URL is the key.which retrieve the values from the particular key


Wednesday, March 19, 2014

Configure with eclipse-Selenium Webdriver and RC

New work-space:


New workbench:


Creating new project:


By clicking java project and give the project name>click next:


Click finish




After clicking finish, it looks as like this…


Next: to create folder within project for importing jar files of Selenium, TestNG


Give the Folder name and click finish


Given folder is present under project:


Download selenium 



Import jars files as copy and paste into the folder
Or
Go to choose folder>right click>build path>configure build path


Click libraries



Click>libraries tab>add external jars and browse the selenium download and click open


Now click>OK
Here: selenium-server-standalone-version is added


Configure build path


Click>OK




Creating package:
File>new>package


Creating class:



Click finish





Adding sample of Selenium RC commands


Here click import Firefox driver


Run as java application:


Before save it else it ask as like this


Thursday, March 13, 2014

Selenium Webdriver- Sample Code

//using the namespace
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            //launch the browser as chrome and add the 
            //location of browser driver
            IWebDriver driver = new ChromeDriver(@"D:\");

            //driver takes to the particular page as 
            driver.Navigate().GoToUrl("http://www.google.com");

            //finding the input element as id
            IWebElement search_box=driver.FindElement(By.Id("gbqfq"));

            //pass the value to element(search_box)
            search_box.SendKeys("selenium webdriver tips and tricks");

            //finding the input element as name
            IWebElement Search_button =driver.FindElement(By.Name("btnG"));

            //click the button(search_button)
            Search_button.Click();

            //getting the title of the browser window
            String title = driver.Title;
           
            //display the title
            Console.Write(title);

            //To close the driver 
            driver.Quit();
        }
    }
}

Selenium Webdriver with visual studio c#


Configuration to start with Selenium Webdriver with c# in visual studio

Step 1:Open the Microsoft Visual Studio

Step 2:File->New->Project

Step 3:Choose Project types as Visual c# and choose Console Application in Templates


Step 4:Name the project(by default it as ConsoleApplication)

Step 5:Click ok

(before proceed the 6th step to download the Selenium Client & WebDriver Language Bindings
in the language of c# in http://docs.seleniumhq.org/download/)

Step 6:Add the reference dll[ by right clicking the (References and Add Reference)in Solution Explorer Window]

Step 7:In Add Reference ->Browse Tab->choose the file of downloaded location
(if the file is in zip format means extract it)
and upload the dll files of either net35 or net40

Step 8:And the download the browser driver that what the browser you want

Step 9:Start to playing with Selenium Webdriver with c# in visual studio