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.
Related
I am only able to execute the 1st test methods. All subsequent test methods fail to execute even though the code is correct. See attached image for error message. Using test.sdk(15.8.0), NUNIT(3.10.1), Selenium.WebDriver(3.13.0), Selenium.IEDriverServer.win64(3.9.0),Selenium.InternetExplorer.WebDriver(3.3.0)
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.IE;
using System;
using OpenQA.Selenium.Interactions;
using System.Threading;
namespace Tests
{
public class LandingPage
{
IWebDriver driver = new InternetExplorerDriver("C:\\Users\\M\\Desktop\\SL\\SLAutomation\\Core\\CoreLandingPage\\CoreLandingPage\\CoreLandingPage\\Drivers\\");
[SetUp]
public void Initialize()
{
driver.Navigate().GoToUrl("http://www.google.com");
Console.WriteLine("Opened URL");
}
[Test]
public void TestCase1()
{
Assert.That(2+2, Is.EqualTo(4));
Console.WriteLine("Test case 1");
}
[Test]
public void TestCase2()
{
Assert.That(2 * 2, Is.EqualTo(4));
Console.WriteLine("Test case 2");
}
[TearDown]
public void CleanUp()
{
driver.Close();
Console.WriteLine("Closed Browser");
}
}
}
You need to instantiate the driver in the method Initialize() tagged with [SetUp]. The error happens because at the end of TestCase1(), CleanUp() is called and the driver is closed. Then TestCase2() comes along and Initialize() is called but the driver no longer exists. You can verify this by commenting out the driver.Close(); line in CleanUp().
Your code should look more like
public class LandingPage
{
IWebDriver driver;
[SetUp]
public void Initialize()
{
driver = new InternetExplorerDriver("C:\\Users\\M\\Desktop\\SL\\SLAutomation\\Core\\CoreLandingPage\\CoreLandingPage\\CoreLandingPage\\Drivers\\");
driver.Navigate().GoToUrl("http://www.google.com");
Console.WriteLine("Opened URL");
}
...
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.
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");
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();
}
}
}
I have two classes in class library
namespace ClassLibrary3
{
public class Class1
{
public string title;
public string author;
public Class1(string title, string author)
{
this.title = title;
this.author = author;
}
}
}
Another class
using System;
using System.Collections;
using System.Linq;
using System.Text;
namespace ClassLibrary3
{
class Class2
{
private Hashtable books;
public Class2()
{
books = new Hashtable();
}
public void addBook(Class1 book)
{
books.Add(book.title, book);
}
public Class1 getBook(String title, String author)
{
return (Class1)books[title];
}
public void removeBook(string title)
{
if (books[title] != null)
books.Remove(title);
}
}
}
And my test is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using System.Collections;
namespace ClassLibrary3
{
[TestFixture]
class TEST
{
[Test]
public void getbooktest()
{
Class1 c1 = new Class1("story", "James");
Class2 c2 = new Class2();
Assert.AreEqual("story", c2.getBook("story", "James"));
}
}
}
Basicly the problem is Nunit doesnt test it. It finds the dll. Loads the test class. But dont come upto the test method.
Please any idea..........
NUnit can't see your TEST class unless you mark it as public, change it to
[TestFixture]
public class TEST
{
...
Side note, consider giving it a better name than TEST ;-)