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();
}
}
}
Related
I'm writting Automatic Test in .NET and I have a little issue with [SetUp] - it's doesn't go before [Test] and Chrome Browser doesn't even open a new window - I have no idea why.
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System.Net;
using System.Threading;
using Tests.Settings;
namespace Tests.TestCaseScenario
{
public class BaseTestCaseTemplate
{
protected string password;
protected string userName;
protected string websideURL;
public IWebDriver Driver = new ChromeDriver();
//load before each test
[SetUp]
public void SetUp()
{
//load userconfig.json
var UserConfigReader = new UserConfigReader();
var CurrentUserConfig = UserConfigReader.LoadJsonConfigToObj(UserConfigPath);
password = CurrentUserConfig.Password;
userName = CurrentUserConfig.UserName;
websideURL = CurrentUserConfig.WebsiteURL;
Driver.Navigate().GoToUrl(websideURL);
Driver.Manage().Window.Maximize();
}
}
}
And here's my test class
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NUnit.Framework;
namespace Tests.TestCaseScenario
{
[TestClass]
public class SimplyChecking : BaseTestCaseTemplate
{
[Test]
public void ApplicationCheckerSimple()
{
HomePage homePage = new HomePage();
homePage.Login(userName, password);
}
}
}
and my Page class:
using OpenQA.Selenium;
using OpenQA.Selenium.Support.PageObjects;
namespace Tests.Pages
{
public class HomePage : BasePageTemplate
{
[FindsBy(How = How.Id, Using = "UserName")]
private IWebElement UserName;
[FindsBy(How = How.Id, Using = "Password")]
private IWebElement Password;
[FindsBy(How = How.ClassName, Using = "btn-primary")]
private IWebElement LoginButton;
public void Login(string user, string password)
{
UserName.SendKeys(user);
Password.SendKeys(password);
LoginButton.Click();
}
}
}
Test do not even run - It doesn't return any value (pass or fail) but when I change [Test] for [TestMethod] it runs and gives a negative result.
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.
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();
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace UnitTestProject1
{
[TestClass]
public class UnitTest1
{
ChromeDriver driver = new ChromeDriver();
[TestMethod]
public void TestMethod1()
{
driver.Navigate().GoToUrl("http:\www.facebook.com");
}
}
}
Use
driver.Navigate().GoToUrl("http://www.facebook.com");
Notice the double // in the URL
Another way is:
driver.Navigate().GoToUrl(#"http:/www.facebook.com");
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