hthomas
hthomas

Reputation: 23

C#, selenium webdriver

I want to create a variable webdriver that i can call in all of my tests. I currently have the following example for display purposes.

Here I define my webdriver I want to use:

using OpenQA.Selenium;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.PhantomJS;

namespace WebAuto
{
    public class OpenBrowser
    {
        private static IWebDriver driver = new ChromeDriver();

        public IWebDriver getDriver()
        {
            return driver;
        }
    }
}

Now i want to call this webdriver in another Test called Login:

using OpenQA.Selenium;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.PhantomJS;


namespace WebAuto
{
    public class Login 
    { 
        public static void Login1() 
    {
            //var driver = new ChromeDriver();
            OpenBrowser.IWebDriver. getDriver();
            driver.Navigate().GoToUrl("www.anywebsite.com");
            //driver.Navigate().GoToUrl("www.anywebsite.com");
            //driver.WaitForPageToLoad();
            var inputtext1 = driver.FindElement(By.Id("lgLogin_txtUserId"));
            //inputtext1.Focus();
            inputtext1.SendKeys("User");
            var inputpassword1 = driver.FindElement(By.Id("lgLogin_txtPassword"));
            //inputpassword1.Focus();
            inputpassword1.SendKeys("Password");
            var inputbutton1 = driver.FindElement(By.Id("btnLoginClient"));
            inputbutton1.Click();
            //driver.WaitForPageToLoad();

        }

    }
}

Could someone please explain what I am missing????

Upvotes: 2

Views: 1453

Answers (1)

Guy
Guy

Reputation: 50909

The way to call the driver from Login class is

OpenBrowser openBrowser = new OpenBrowser();
IWebDriver driver = openBrowser.getDriver();

Upvotes: 3

Related Questions