Selenium2.0 WebDriver Automation Using C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium;
using System.Drawing;
namespace WebDriver
{
    class Program
    {
        static void Main(string[] args)
        {
            string baseUrl = @"https://www.amazon.com";
            string userName = @"xxxxxxxxxxxxxxxxxxxxx";
            string ASIN = "1430225491";
            IWebDriver driver = new FirefoxDriver();
            for (int i = 0; i < 1; i++)
            {
                //Login
                driver.Navigate().GoToUrl(baseUrl + @"/gp/css/homepage.html?ie=UTF8&ref_=topnav_ya");
                System.Threading.Thread.Sleep(5000);
                if (driver.PageSource.Contains("Sign out"))
                {
                    driver.FindElement(By.LinkText("Sign out")).Click();
                    System.Threading.Thread.Sleep(5000);
                }
                driver.FindElement(By.Name("email")).SendKeys(userName);
                driver.FindElement(By.Name("password")).SendKeys("xxxxxxxx");
                driver.FindElement(By.XPath("//input[contains(@value, 'Sign In')]")).Click();
                System.Threading.Thread.Sleep(5000);
                //Load the Detail Page
                driver.Navigate().GoToUrl(baseUrl + "/dp/" + ASIN);
                System.Threading.Thread.Sleep(5000);
                //Add to Cart
                driver.FindElement(By.Name("submit.add-to-cart")).Click();
                System.Threading.Thread.Sleep(5000);
                //Navigate to Non Empty Shopping Cart
                driver.Navigate().GoToUrl(baseUrl + "/gp/cart/view.html/ref=gno_cart");
                System.Threading.Thread.Sleep(5000);
                while (driver.PageSource.Contains("delete.1"))
                {
                    driver.FindElement(By.XPath("//input[contains(@name, 'delete.1')]")).Click();
                    System.Threading.Thread.Sleep(5000);
                    //Verify that the shopping cart is indeed empty
                    if (driver.PageSource.Contains("Your Shopping Cart is empty"))
                    {
                        Console.WriteLine("Shopping Cart is now empty");
                    }
                }
                driver.FindElement(By.XPath("//img[contains(@alt, 'continue shopping')]")).Click();
                System.Threading.Thread.Sleep(5000);
                //Select Books from the combo box
                string optionToSelect = "Electronics";
                IWebElement selectElement = driver.FindElement(By.Id("searchDropdownBox"));
                System.Collections.ObjectModel.ReadOnlyCollection<IWebElement> componentList = selectElement.FindElements(By.TagName("option"));
                foreach (IWebElement component in componentList)
                {
                    Console.WriteLine(component.Text);
                    if (component.Text.Contains(optionToSelect))
                    {
                        component.Click();
                        Console.WriteLine(optionToSelect + " selected");
                        break;
                    }
                    else
                    {
                        Console.WriteLine("Option not found");
                    }
                }
                //Click the Go button
                driver.FindElement(By.XPath("//input[contains(@alt, 'Go')]")).Click();
                System.Threading.Thread.Sleep(5000);
                while (driver.PageSource.Contains("Next »"))
                {
                    driver.FindElement(By.LinkText("Next »")).Click();
                    System.Threading.Thread.Sleep(5000);
                    Console.WriteLine(driver.Url);
                }
                //Navigate to Your Account
                driver.Navigate().GoToUrl(baseUrl + "/gp/css/homepage.html/ref=topnav_ya");
                System.Threading.Thread.Sleep(5000);
                //Sign Out
                driver.FindElement(By.LinkText("Sign out")).Click();
                System.Threading.Thread.Sleep(5000);
                OpenQA.Selenium.Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
                string fileName = "test" + i.ToString() + ".jpg";
                ss.SaveAsFile(@"C:\Selenium\toDelete\" + fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            driver.Quit();
        }
    }
}
Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s