C#, selenium webdriver - c#

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????

The way to call the driver from Login class is
OpenBrowser openBrowser = new OpenBrowser();
IWebDriver driver = openBrowser.getDriver();

Related

I am trying to run a test on multiple Browsers using MSTest and Selenium

I am simultaneously teaching myself Selenium 3.0 and Programming to learn how to Automate my tests (previously a manual tester with no IT background, fell into testing via UAT).
I have successfully written a handful of tests all of which work :). I can define which browser to run the test set on, but cant run the test set on multiple browsers as 1 test run. (I have to define the browser in my Test Initialise and comment out the other browsers). When I try to run the test on 2 browsers at once Browser A opens but nothing runs, Browser B then opens and runs the tests.
I have looked through the posts on this site and on google generally but have not found a clear answer to how to get this to work using MSTest and Semenium in C#
My Code
One of my tests
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Kukd_Consumer_Tests;
namespace Kukd_Consumer_Test
{
//login from Hompage
[TestClass]
public class Login : Consumer_Standard_Functionality
{
[TestMethod]
public void User_Can_Login()
{
LoginPage.LoginAs("Richard.Cariven#Kukd.Com").WithPassword("Test1234").Login();
Assert.IsTrue(AccountPageAssert.IsAtAccountPage("Hi Richard"), "Failed to login");
}
}
}
My std Functionality Test Class (Initialise and Cleanup steps)
using Kukd_Consumer_Tests;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Kukd_Consumer_Test
{
public class Consumer_Standard_Functionality
{
[TestInitialize]
public void Init()
{
driver.InitializeChrome();
driver.InitializeFireFox();
Homepage.GoTo_HomePage();
}
[TestCleanup]
public void Cleanup()
{
driver.Quit();
}
}
}
My driver class
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Remote;
using System;
namespace Kukd_Consumer_Tests
{
public class driver
{
public static IWebDriver Instance { get; set; }
public static void InitializeChrome()
{
Instance = new ChromeDriver(#"C:\Users\xxxxxxxxx.xxxxxxxx\Documents\Visual Studio 2015\Drivers\Chrome");
Instance.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
}
public static void InitializeFireFox()
{
Instance = new FirefoxDriver();
Instance.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
public static void Quit()
{
Instance.Quit();
}
}
}
My LoginPage class
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Kukd_Consumer_Tests
{
public class LoginPage
{
public static LoginCommand LoginAs(string userName)
{
return new LoginCommand(userName);
}
public class LoginCommand
{
private readonly string userName;
private string password;
public LoginCommand(string userName)
{
this.userName = userName;
}
public LoginCommand WithPassword(string password)
{
this.password = password;
return this;
}
public void Login()
{
var accountButton = driver.Instance.FindElement(By.LinkText("My Account"));
accountButton.Click();
var loginInput = driver.Instance.FindElement(By.Id("login-email"));
loginInput.SendKeys(userName);
var passwordInput = driver.Instance.FindElement(By.Id("login-password"));
passwordInput.SendKeys(password);
var loginButton = driver.Instance.FindElement(By.Id("loginBtn"));
loginButton.Click();
}
}
}
}
When I run the above Chrome opens a blank window and does nothing more. Firefox opens the homepage, then runs the test as expected and closes the window when done.

WCF with Duplex Communication

Whenever I am using Window Forms It works Fine But it always give error with console applicion.
Error- The socket connection was aborted. This could be caused by an
error processing your message or a receive timeout being exceeded by
the remote host, or an underlying network resource issue. Local socket
timeout was '00:01:00'.
Here is My Code
Contract
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace ClassLibrary1
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IReportService" in both code and config file together.
[ServiceContract(CallbackContract=typeof(IReportServiceCallbak))]
public interface IReportService
{
[OperationContract(IsOneWay=true)]
void ProcessReport();
}
public interface IReportServiceCallbak
{
[OperationContract(IsOneWay=true)]
void Progress(int percentage);
}
}
Service
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Threading;
namespace ClassLibrary1
{
public class ReportService : IReportService
{
public void ProcessReport()
{
for (int i = 0; i < 100; i++)
{
Thread.Sleep(50);
OperationContext.Current.GetCallbackChannel<IReportServiceCallbak>().Progress(i);
}
}
}
}
Client
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Threading.Tasks;
using System.Threading;
namespace DuplexClientsss
{
class Program
{
static void Main(string[] args)
{
new Tests();
} }
class Tests : ReportService.IReportServiceCallback
{
ReportService.ReportServiceClient obj;
public Tests()
{
obj = new ReportService.ReportServiceClient(new InstanceContext(this));
obj.ProcessReport();
}
public void Progress(int percentage)
{
Console.WriteLine(percentage);
}
}
}
new Task
(
()=>
{
obj.ProcessReport();
}
).Start();

Asserting an actual value in C# using selenium and Nunit

HI I'm using Nunit and Selenium web driver. I've used Java previously in the same context Im struggling to solve this line of code.
`string actualvalue = IWebElement searchInput =` `driver.FindElement(By.Id("sb_form_q"));`
Here's the rest of it.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using NUnit.Framework;
namespace SeleniumTests1
{
[TestFixture]
class SeleniumTest
{
[Test]
public void Main(string[] args)
{
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://www.bing.com/");
driver.Manage().Window.Maximize();
string actualvalue = IWebElement searchInput = driver.FindElement(By.Id("sb_form_q"));
searchInput.SendKeys("Hello World");
searchInput.SendKeys(Keys.Enter);
Assert.AreEqual(actualvalue, "Hello World");
driver.Close();
}
}
}
This works for me:
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
namespace ConsoleApplication1
{
public static class SeleniumTest
{
public static void Main(string[] args)
{
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://www.bing.com/");
driver.Manage().Window.Maximize();
IWebElement searchInput = driver.FindElement(By.Id("sb_form_q"));
searchInput.SendKeys("Hello World");
searchInput.SendKeys(Keys.Enter);
searchInput = driver.FindElement(By.Id("sb_form_q"));
string actualvalue = searchInput.GetAttribute("value");
Assert.AreEqual(actualvalue, "Hello World");
driver.Close();
}
}
}

WebDriver i am trying to use isElementPresent but error shows not exist in the current context

I am new to WebDriver and writing this code in C# Visual Studio (code snippet below)
I am verifying if a text field is present on the home page using IsElementPresent.
I get the error The name IsElementPresent does not exist in the current context.
What am i doing wrong?
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Threading;
using System.Linq;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
namespace Homepage_check2
{
[TestFixture]
public class Driver
{
IWebDriver driver;
[SetUp]
public void Setup()
{
// Create a new instance of the Firefox driver
driver = new FirefoxDriver();
}
[TearDown]
public void Teardown()
{
driver.Quit();
}
[Test]
public void homepage()
{
//Navigate to the site
driver.Navigate().GoToUrl("http://www.milkround.com");
Assert.IsTrue(IsElementPresent(By.Id("ctl00_uxToolbar_uxQueryTextBoxToolbar")));
}
catch
{
//verificationErrors.Append(e.Message);
}
}
}
}
Where does "IsElementPresent" come from? I have never seen that used in WebDriver.
In WebDriver you need to do wrap a try catch around the findElement method.
e.g
Boolean elementDisplayed;
try {
WebElement element = driver.findElement(By.Id("ctl00_uxToolbar_uxQueryTextBoxToolbar"));
elementDisplayed = element.displayed;
}
catch (NoSuchElementException e) {
elementDisplayed = false;
}
Obviously you can wrap this in a helper method of some kind of perhaps add it to the WebDriver classes.
I'll leave that to you, but this is the general idea

How to write an app-launching application with arguments in C#?

How to if I want to write an application that launches Firefox with arguments ?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace Launcher
{
public static class Program
{
public static void Main(string[] args)
{
Process.Start("C:/Program Files/Mozilla Firefox/firefox.exe");//this is ok
Process.Start("C:/Program Files/Mozilla Firefox/firefox.exe -P MyProfile -no-remote");// this doesn't work
}
}
}
You will need to specify the process.StartInfo.Arguments
See this question: Calling an application from ASP.NET MVC
You will need to use the process.StartInfo.Arguments, as shown here:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace Launcher
{
public static class Program
{
public static void Main(string[] args)
{
Process firefox = new Process();
firefox.StartInfo.FileName = #"C:\Program Files\Mozilla Firefox\firefox.exe";
firefox.StartInfo.Arguments = "-P MyProfile -no-remote";
firefox.Start();
}
}
}

Categories

Resources