I am new to automation testing and selenium and have been watching alot of selenium tutorials. I realized that selenium test cases are written in 2 formats and im not sure which one to go with.
1)
namespace SeleniumTests
{
[TestFixture]
public class Login
{
private IWebDriver driver;
private StringBuilder verificationErrors;
private string baseURL;
private bool acceptNextAlert = true;
[SetUp]
public void SetupTest()
{
driver = new FirefoxDriver();
baseURL = "http://chapters.com";
verificationErrors = new StringBuilder();
}
[TearDown]
public void TeardownTest()
{
try
{
driver.Quit();
}
catch (Exception)
{
// Ignore errors if unable to close the browser
}
Assert.AreEqual("", verificationErrors.ToString());
}
[Test]
public void TheLoginTest()
{
driver.Navigate().GoToUrl(baseURL");
driver.FindElement(By.Id("loginCtrl_UserName")).Clear();
driver.FindElement(By.Id("loginCtrl_UserName")).SendKeys("operations");
driver.FindElement(By.Id("loginCtrl_Password")).Clear();
driver.FindElement(By.Id("loginCtrl_Password")).SendKeys("welcome");
driver.FindElement(By.Id("loginCtrl_LoginButton")).Click();
driver.FindElement(By.Id("btnInitialLoad")).Click();
Assert.AreEqual("Chapters", driver.Title);
}
}
}
2)
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
namespace TestProject1
{
public class UnitTest1
{
public void main()
{
FirefoxDriver driver = new FirefoxDriver();
string baseURL = "http://seleniumhq.org/";
driver.Navigate().GoToUrl(baseURL);
driver.FindElement(By.LinkText("Projects")).Click();
driver.FindElement(By.LinkText("Selenium IDE")).Click();
Assert.AreEqual(driver.FindElement(By.XPath("//div[#id='mainContent']/table/tbody/tr/td/p/b")).Text, "Selenium IDE");
driver.Close();
}
}
}
Which one do I go with?
Thanks
The second scenario is simple sequential flow of statements without added advantage of any frameworks like Junit,TestNG,Nunit. It is good for people who have just started with Selenium 2.0 and want to practice with different methods provided by webdriver.
The first scenario is logical division of the code into different annotations provided by Nunit, with these annotations one can get tremendous power of the underlining framework being used, in your case Nunit, these annotation are automatically called by the Nunit framework in a defined order. Apart from this there are multliple other functionalities provided by these frameworks like Reporting,Assertions,Support for Mock Objects,etc
Always use the first scenario for writing Selenium code because along with understanding of the webdriver code, one also gets hang of the underlining framework.
Related
I want to take screenshot of a failed test case. But I don't know how to force Nunit to use my listener.
I was trying to use IAddins, but Nunit doesn't have NUnit.Core.Extensibility lib.
My code:
using System;
using OpenQA.Selenium;
using NUnit.Framework.Interfaces;
using AT_MentoringPortal.Driver;
using System.Drawing.Imaging;
namespace AT_MentoringPortal.listeners
{
public class ScreenshotListener : ITestListener
{
private readonly string path = ".//screens//";
public void TestFinished(ITestResult result)
{
if (result.ResultState.Status == TestStatus.Failed)
{
IWebDriver driver = DriverFactory.GetDriver();
this.MakeScreenshot(driver, result.Name);
}
}
public void MakeScreenshot(IWebDriver driver, string testName)
{
string timestamp = DateTime.Now.ToString("yyyy-MM-dd-hhmm-ss");
var screenshot = ((ITakesScreenshot)driver).GetScreenshot();
screenshot.SaveAsFile($"{this.path}{timestamp} {testName}", ImageFormat.Jpeg);
}
public void TestOutput(TestOutput output)
{
// throw new NotImplementedException();
}
public void TestStarted(ITest test)
{
// throw new NotImplementedException();
}
}
}
Please, show me how to start my listener in test class.
ITestListener is an internal interface used by NUnit itself in running tests. There was a similar interface in NUnit V2 (TestListener) and you were able to create addins that used it. NUnit 3 doesn't have addins in the way that NUnit 2 did, although it can be extended in other ways.
Did you want to save a screen shot for only certain tests? Or for each test in a certain fixture? Or more generally?
To do it within a fixture, you could use a OneTimeTearDown method.
I'm having issues setting up my tests. I have tried using a console c# file for my selenium tests which runs the test however it doesn't appear in the test explorer. When I create a unit test c# project it doesn't run or show up in the test explorer. What have done wrong?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace SeleniumTests1
{
[TestClass]
class SeleniumTest
{
[TestMethod]
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();
}
}
}
This may work. I think your TestMethod needs to be public and non-static in order for it to appear in Test Explorer.
namespace SeleniumTests1
{
[TestClass]
public class SeleniumTest
{
[TestMethod]
public void Main()
{
You're making a Main method the test method?
Make a separate test project, then reference the project you're testing and move your code to that instead e.g.
namespace Tests
{
[TestClass]
public class MyProjTests
{
[TestMethod]
public void Test{
//your code
}
}
}
Although this one's a rather obvious and straightforward answer, but, looking at the code posted originally by Peter and my own silly mistake I realized that one more place where we can go wrong is by not making our outer test class public without which they would default to internal and the contained tests would not show up in the test explorer. So, the final form of a unit test code would begin with something like this
namespace SeleniumTests1
{
[TestClass]
public class SeleniumTest
{
[TestMethod]
public void testMethod(string[] args)
{
I think I may have managed to resolve the issue in this instance by importing the 4 dlls from the net40 file from the selenium website.
Using Selenium C# web driver with NUnit for automation. I am generating Allure report using command line and my report gets fantastically created but I need help on the following issue:
I have the following structure using Page object model (2 Test and 1 Page). Now when I see the report it shows at the top Test run (2 testsuites, 2 testcases) and each testcase is a testsuite. I want it to say 1 testsuites, 2 testcases. How do I do that?
namespace ApplicationName.TestCases
{
[TestFixture]
class VerifyCreateOrder
{
IWebDriver driver;
[SetUp]
public void Initialize()
{
driver = new FirefoxDriver();
}
[TestCase]
public void doCreateOrder()
{
LoginPage loginPage = new LoginPage();
//some Assertion
}
}
}
namespace ApplicationName.TestCases
{
[TestFixture]
class SearchOrder
{
IWebDriver driver;
[SetUp]
public void Initialize()
{
driver = new FirefoxDriver();
}
[TestCase]
public void doSearchOrder()
{
LoginPage loginPage = new LoginPage();
//some Assertion
}
}
}
The below is my LoginPage Page object:
namespace ApplicationName.Pages
{
class LoginPage
{
public void doLogin(IWebDriver driver, String username, String password)
{
driver.Navigate().GoToUrl("http://www.myxyzsite.com");
driver.FindElement(By.Id("xyz")).SendKeys(username);
driver.FindElement(By.Id("xyz")).SendKeys(password);
driver.FindElement(By.Id("xyz")).Click();
}
}
}
I read about the NUnit suite attribute at http://www.nunit.org/index.php?p=suite&r=2.5.5 and created a c# class with enumerator as described but how do i call it/wire it? What changes do I need to make for my test classes?
namespace NUnit.Tests
{
public class MyTestSuite
{
[Suite]
public static IEnumerable Suite
{
get
{
ArrayList suite = new ArrayList();
suite.Add(new VerifyCreateOrder());
suite.Add(new SearchOrder());
return suite;
}
}
}
}
I want it to say 1 testsuites, 2 testcases. How do I do that?
Without adding a Suite or similar, you could put both Test cases into the same TestFixture, since that's what the testsuite output is built from. You may be able to do that using a partial class, or you can simply conflate the two classes. However, your Suite solution is a better choice.
What changes do I need to make for my test classes?
Call NUnit with the option /fixture:NUnit.Tests.MyTestSuite.
Note that all of this has changed with NUnit 3 and the Suite attribute is gone. I can't see any way to do what you want in NUnit 3 short of reorganizing your test cases.
If it's very important to merge tests into suites, you can use XSLT. The NUnit test result schema is quite straightforward and easy to manipulate using XSLT.
I'm a beginner in C# and automation. I want to try automate IE with Selenium and NUnit. I was able to lunch IE and navigate to google.com. But from some reason the prog don't find the elementID. Therefore I can't "send keys" to the field. My other problem is how do I submit a form that has no ID or Name.
Here is the code :
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium;
using NUnit.Framework;
using OpenQA.Selenium.Internal;
namespace ClassLibrary1
{
public class Class1
{
public IWebDriver driver;
public string baseUrl;
[SetUp]
public void Setup()
{
baseUrl ="https://www.google.com";
driver = new InternetExplorerDriver();
}
[Test]
public void TestCase1()
{
driver.Navigate().GoToUrl(baseUrl);
System.Threading.Thread.Sleep(500);
driver.FindElement(By.Id("gbqfq")).SendKeys("WhatIsMyIP");
System.Threading.Thread.Sleep(500);
driver.FindElement(By.Id("gbqfba")).Click();
}
[TearDown]
public void TearDown() { }
}
}
I've experienced this issue before with google search. The thing that happens is, as your script sends the keys to get textbox, the "smart search" thing from google comes up, and the element disappears.
Try this, this should work.
[Test]
public void TestCase1()
{
driver.Navigate().GoToUrl(baseUrl);
driver.FindElement(By.Name("q")).SendKeys("WhatIsMyIP");
driver.FindElement(By.Name("btnG")).Click();
}
Observe it for yourself. Perform a search, and watch the DOM.
i created a little script with selenium recorder and i exported it to a c# file,
selenium created so many code lines on it but some attributes like:
[TestFixture]
public class TestinGSelenium
{
private IWebDriver driver;
private StringBuilder verificationErrors;
private string baseURL;
private bool acceptNextAlert = true;
[SetUp]
public void SetupTest()
{
driver = new FirefoxDriver();
baseURL = "http://www.google.it/";
verificationErrors = new StringBuilder();
}
[TearDown]
public void TeardownTest()
{
try
{
driver.Quit();
}
catch (Exception)
{
// Ignore errors if unable to close the browser
}
NUnit.Core.NUnitFramework.Assert.AreEqual("", verificationErrors.ToString());
}
visual studio cannot verify attributes
about :[TestFixture] [TearDown] [SetUp]
i added a reference to: "NUnit" and "selenium" but continues to return error .
what else can i do? to run this c# script?
thanks for any suggestion
Adding a reference is not enough.
You will also need to add a using for NUnit.Framework and OpenQA.Selenium and OpenQA.Selenium.Firefox:
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using NUnit.Framework;
As you are using Visual Studio, it also has a handy tip - right-click on one of the attributes it is complaining about, and it will give you a quick handy option to add the reference & using on your behalf.