unknow attribute in a file created from selenium - c#

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.

Related

Nunit. Take screenshot on test failure using ITestListener

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.

Having issues initialising my selenium tests using the [TestInitialize]

I've been attempting to learn selenium in c# and in the past day or so it's developed into using the testinitialize. Before I ran everything from my test method and it worked great the long term goal is for me to be able to load a page on the initialize then login in one test add posts on another test etc. I don't want to be loading up and logging in every time I'd like it to be free flowing from the point of logging in. At the moment I'm getting something in the wrong place as now it's just firing up a blank firefox page and doing nothing. I've kept it simple at the moment so I can get to grips with it. So the code below should load up wikipedia and check for some text in the heading.
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
namespace SeleniumPractice
{
[TestClass]
public class Setup
{
IWebDriver driver;
[TestInitialize]
public void GoToWiki()
{
//Create an instance of the firefox driver.
IWebDriver driver = new FirefoxDriver();
}
[TestMethod]
public void VerifyHelloWorld()
{
driver.Navigate().GoToUrl("https://en.wikipedia.org/wiki/%22Hello,_World!%22_program");
driver.Manage().Window.Maximize();
string actualvalue = driver.FindElement(By.Id("firstHeading")).Text;
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
Assert.AreEqual(actualvalue, "\"Hello, World!\" program");
}
[TestCleanup]
public void Teardown()
{
driver.Close();
}
}
}
Also I'm getting a green line under IwebDriver driver; in my class. With this error.
field 'seleniumPractice.Setup.driver' is never assigned to, and always have its default value null
I put this here because i noticed the test method didn't recognise the driver anymore when I moved it out of the test method.
The reason for the compiler warning and the blank Firefox window is that you are not actually assigning the driver field with a reference to a new FirefoxDriver object in GoToWiki(), you are actually declaring a new variable that is scoped only to that method and assigning a reference to a new FirefoxDriver object to that variable. The field is null when you call GoToUrl on it in VerifyHelloWorld(). Try this edit:
[TestInitialize]
public void GoToWiki()
{
//Create an instance of the firefox driver.
driver = new FirefoxDriver();
}

How to distinguish between testsuite and testcase on the report

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.

Selenium Test Case writing practice

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.

Selenium IE Automation

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.

Categories

Resources