How to execute concurrent tests with Selenium WebDriver on a single machine? - selenium

My goal is to run multiple tests on a single machine in parallel. For that I'm using Selenium WebDriver with Firefox and Mbunit. Unfortunately it looks like like driver creation in Selenium is not thread safe and I have to wrap this part of the code with global lock. This is not the end of the world but my question is if this is all I need to do or maybe there are other parts that need synchronization? Another option would be to have AppDomain or Process isolation in MBUnit but I'm not sure if this is implemented.
[TestFixture]
[Parallelizable]
public class Class1
{
public static object padlock = new object();
[Test]
[Parallelizable]
public void Test1()
{
var driver = CreateDriver();
driver.Navigate().GoToUrl("http://www.mozilla.org");
driver.FindElementByCssSelector("a[href='/projects/']").Click();
Thread.Sleep(TimeSpan.FromSeconds(5));
driver.Quit();
}
[Test]
[Parallelizable]
public void Test3()
{
var driver = CreateDriver();
driver.Navigate().GoToUrl("http://www.mozilla.org");
driver.FindElementByCssSelector("a[href='/contribute/']").Click();
Thread.Sleep(TimeSpan.FromSeconds(5));
driver.Quit();
}
**private FirefoxDriver CreateDriver()
{
lock(padlock)
{
return new FirefoxDriver();
}
}**
}

I've been using MbUnit and Selenium in parallel, and I can assure you MbUnit is completely thread safe, and works perfectly once you instantiate WebDriver correctly. You should be fine with just the lock.
I would like to point out that using your code example your tests will not fail correctly. On any failed assertion or thrown exception you will not reach the Quit() section of your code. This is why the Setup/Teardown methods are typically used to start/stop the browser.
FYI, You can still use the setup/teardown methods in parallel, you just need a way of storing/referencing the Driver. You can use an IDictionary referenced by TestStep name.

I don't know how work MBunit, but there are differences between cocnurency and parallelism.
My opinion it's that an selenium test can be well integrated in concept of parallelism execution. Anyway, discution can be confusing.
So, how can be done.
1. Create an class that implements Runnable or extends Thread class. This class will launch test, something like this:
class MyClass implements Runnable
{
private Thread t;
public MyClass()
{
t=new Thread (this);
t.start();
}
#Override
public void run() {
WebDriver w = new FirefoxDriver();
// begin your test
}
}
In Main class, create multiple instance of MyClass. Each of them will launch an test into it's own thread.
Here it's link for Thread class documentation: http://docs.oracle.com/javase/1.3/docs/api/java/lang/Thread.html

Related

How to log unique statements for each tests in the ExtentReport with the implementation of ITestListener?

I am currently using the ExtentReport framework to generate a comprehensive test report after TestNG completes its suite of test class using Selenium.
To make the process easier and to reduce code, I am using a listener class to automate the Test Success and Test Failure scenerios such that in case of every success/failure, there will some actions done to the report automatically. The listener implementation goes like this:
public class Listeners extends Base implements ITestListener{
ExtentTest test;
ExtentReports extent=ExtentReporterNG.getReportObject();
**ThreadLocal**<ExtentTest> extentTest =new **ThreadLocal**<ExtentTest>();
public void onTestStart(ITestResult result) {
// TODO Auto-generated method stub
test= extent.createTest(result.getMethod().getMethodName());
extentTest.set(test);
}
public void onTestSuccess(ITestResult result) {
takeScreenShotandAttachtoReport();
extentTest().get().log("Success");
}
public void onTestFailure(ITestResult result) {
takeScreenShotandAttachtoReport();
extentTest().get().log("Failure");
}
public void onFinish(ITestContext context) {
// TODO Auto-generated method stub
extent.flush();
}
As above, with every test case, an ExtentTest ThreadLocal would be instantiated along the interface methods, which would execute with respect to their individual listeners based on the test results. This reduces the extra code that are needed for the same implementation if ExtentReport was to be instantiated on every test class. However, this implementation has its own hurdle: It would not allow variations (logging, individual test result customization, etc). Say I'd want some ExtentReport logging to be done for each unique test case, I have no ways to invoke the listener and do any type of logging for that particular test.
Is there anyway that this can be solved?

How can I control how many JUnit5 Test Instances are going to be generated in Runtime and apply separate ParameterResolver to each of the instance

I'd like to execute Selenium Grid tests using Maven like this:
mvn verify-Dtest=BaseTest -Dprop.selenium.server.url=http://localhost:4444/wd/hub
-Dprop.browser=chrome
-Dprop.version=80.0.3987.106
I inject ChromeDriver into Test constructor using JUnit5 ParameterResolver interface
#ExtendWith(ChromeRemoteWebDriverParameterResolver.class)
#TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class MultiBrowserDemoTest {
RemoteWebDriver driver;
public MultiBrowserDemoTest(RemoteDriver driver) {
this.driver = driver.getDriver();
}
#SneakyThrows
#Test
public void testGrid() {
driver.get("https://www.google.com/");
WebElement search = driver.findElement(By.name("q"));
search.sendKeys("JUnit5 extensions");
search.submit();
}
#AfterAll()
void tearDown() {
driver.quit();
}
}
It works fine. But I can't see how to implement multi-browser test execution.
Let's say, I want to add multiple browsers -Dprop.browser=chrome, firefox, opera, ie11
I created multiple classes implementing ParameterResolver Interface. But JUnit5 does not let me inject them all into my Test Class. It does not create new instances of Test class either.
I tried to use TestInstanceFactory to create new Instances of my Test class and apply separate implementation of ParameterResolver interface, but it did not work for me.
End Result: I can run the same test in multiple browsers in parallel using Selenium Grid and only one Test Class that I will be able to instantiate multiple times with separate webdriver.
If I understood your scenario correctly, what you are asking for is support for what we call "parameterized containers" (i.e., something analogous to #ParameterizedTest but at the class level), which does not exist yet in JUnit Jupiter.
See JUnit Jupiter issues #871 and #878 for details.

How did TestNg annotation mentioned in one class get executed from another from another class?

While learning Testng on Udemy, I come across a code which I am unable to understand. Instructor has created class named "testcore" where he defined #BeforeMethod/#aftermethod.Later he created another class named "LoginTest" where he wrote actual test with #test. He extended testcore class in loginTest to get variable initiated in testcore class. When he ran loginTest then #BeforeMethod/#aftermethod also ran with this. How did these two method ran along with #test when these methods are in different class.
here are both codes:
public class testcore {
public static Properties config = new Properties();
public static Properties obj = new Properties();
public static Xls_Reader xls = null;
public static WebDriver driver;//=null;
#BeforeMethod
public void init() throws Exception{
if(driver==null) {
// Loading Config Properties File
File Config_f = new File(System.getProperty("user.dir")+"\\src\\dd_Properties\\config.properties");
FileInputStream fs = new FileInputStream(Config_f);
config.load(fs);
// Loading Object Properties File
File Obj_f = new File(System.getProperty("user.dir")+"\\src\\dd_Properties\\Object.properties");
fs = new FileInputStream(Obj_f);
obj.load(fs);
//Loading xlsx file
xls = new Xls_Reader(System.getProperty("user.dir")+"\\src\\dd_Properties\\Data.xlsx");
System.out.println(config.getProperty("browerName"));
if(config.getProperty("browerName").equalsIgnoreCase("Firefox")) {
driver = new FirefoxDriver();
}
else if(config.getProperty("browerName").equalsIgnoreCase("Chrome")) {
driver = new ChromeDriver();
}
else {
throw new Exception("Wrong/No Browswer sepcified");
}
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
}
#AfterMethod
public void quitdriver() throws EmailException {
driver.quit();
//monitoringMail.Sendmail();
}
Here are LoginTest class:
public class LoginTest extends testcore {
#Test
public void doLogin() {
driver.findElement(By.xpath(obj.getProperty("LoginBtn"))).click();
driver.findElement(By.xpath(obj.getProperty("username"))).sendKeys();
driver.findElement(By.xpath(obj.getProperty("password"))).sendKeys();
}
1) How did these two method ran along with #test.
First of all LoginTest extends the testcore .
Due to this we can inherit the method of testcore in our LoginTest class.
2) When he ran loginTest then #BeforeMethod/#aftermethod also ran with this.
Please refer below details
Configuration information for a TestNG class:
#BeforeSuite: The annotated method will be run before all tests in this suite have run.
#AfterSuite: The annotated method will be run after all tests in this suite have run.
#BeforeTest: The annotated method will be run before any test method belonging to the classes inside the <test> tag is run.
#AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the <test> tag have run.
#BeforeGroups: The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked.
#AfterGroups: The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked.
#BeforeClass: The annotated method will be run before the first test method in the current class is invoked.
#AfterClass: The annotated method will be run after all the test methods in the current class have been run.
#BeforeMethod: The annotated method will be run before each test method.
#AfterMethod: The annotated method will be run after each test method.
It's very simple inheritance related question. When you have extended testcore class in LoginTest class all the methods and data member available in parent will be available to child class including methods annotated with #Before Method etc.
I think you are confused due to miss concept regarding the way TestNG run a program. There are different different way to run a testNG annotated program to get executed and among them XML is a way. So don't get get confuse that all class, methods need to include in that xml. You just need an entry point only and rest will call accordingly.

Assert in Selenium C#

Is there a Assert class present in Selenium C# just like we have in Coded UI test.
Or I should use the Microsoft.VisualStudio.TestTools.UnitTesting.Assert class to perform asserts in Selenium?
Yes, you would use the Assert class in your unit test framework, in your case MSTest.
The Selenium library doesn't have responsibility over test framework type of functions, including asserts.
You can use FluentAssertions which supports many different frameworks including MSTest which might minimize changes needed if you need to switch frameworks for any reason.
The Assert class is available with either the MSTest or NUnit framework.
I used NUnit, and there is the Assert class as in below line of code.
Sample code:
Assert.AreEqual(AmericaEmail, "SupportUsa#Mail.com", "Strings are not matching");
According to https://msdn.microsoft.com/en-us/library/ms182532.aspx
[TestClass]
public class UnitTest1
{
private IWebDriver driver;
[TestInitialize]
public void Setup()
{
driver = new ChromeDriver();
driver.Url = "Your URL";
}
[TestMethod]
public void TestMethod1()
{
//Your first test method
var element = driver.FindElement(By.Id("ID"));
Assert.IsTrue(element.Displayed);
Assert.AreEqual(element.Text.ToLower(), "Expected text".ToLower());
}
[TestMethod]
public void TestMethod2()
{
//Your second test method
}
[TestCleanup]
public void TearDown()
{
driver.Quit();
}
}
You can use MSTest or I would rather prefer to write simple assertion method. In Selenium, most of the use cases would be a Boolean check, IsTrue | IsFalse (you can even extend to write more complex assertions), so when you define your own assertion you will get more control of your script, like,
Taking a screenshot if the assertion fails
You can live with the failure and continue the test
You can mark the script as partially passing
Extract more information from the UI, like JavaScript errors or server errors
To use Assert you have to first create the unit testing project in Visual Studio.
Or import the following reference to the project.
using Microsoft.VisualStudio.TestTools.UnitTesting;
// Using this you can use the Assert class.
Assert.IsTrue(bool);
Assert.IsFalse(bool);
Assert.AreEqual(string,string);
Assert.Equals(obj1, obj2); // Object comparison
Assert.AreEqual(HomeUrl, driver.Url); // Overloaded, comparison. Same object value
Assert.AreNotEqual(HomeUrl, driver.Url);
Assert.AreSame("https://www.google.com", URL); // For the same object reference
Assert.IsTrue(driver.WindowHandles.Count.Equals(2));
Assert.IsFalse(driver.WindowHandles.Count.Equals(2));
Assert.IsNull(URL); Assert.IsNotNull(URL);
Selenium C# is not offering an assert class. You have to borrow it from somewhere or write your own implementation.
Writing your own implementation will give you more freedom to manage assertions, as Peter said.
Here is the basic idea. Just create a static class having assert methods like this:
public static class MyAssertClass
{
public static void MyAreEqualMethod(string string1, string string2)
{
if (string1 != string2)
{
// Throw an exception
}
}
}
MSTest Framework : Assert Class
https://learn.microsoft.com/en-us/dotnet/api/microsoft.visualstudio.testtools.unittesting.assert?view=mstest-net-1.3.2
This way assert can be used for switching into popup's displayed:
IAlert alert = driver.SwitchTo().Alert();
String alertcontent = alert.Text;
Assert.AreEqual(alertcontent, "Do you want to save this article as draft?");
obj.waitfn(5000);
alert.Accept();

How JUnit TestCase functionality actually works?

I have a code like this:
public class MyTest extends TestCase {
private MyObject mObject1;
private MyObject mObject2;
...
#Override
public void setUp() throws Exception {
super.setUp();
}
public void testSomething() {
mObject1 = new MyObject();
mObject2 = new MyObject();
}
public void testSomething2() {
// Here I can't access the previously created objects mObject1 and
// mObject2, because they are again null.
// Why is that, if *my* setUp() method doesn't touch them?
}
My guess is that JUnit instantiates the class again every time. Can someone please explain me the workflow?
Thanks.
JUnit will instantiate the class (MyTest) once per test and then execute the methods
setUp()
testXXX()
tearDown()
until it runs all the methods that start with test and don't receive any parameters. So in your example, Junit will instantiate MyTest twice. You can read more about this in the JUnit documentation.
Bear in mind that this is the old way of writing tests. From Junit 4 (I think) the preferred way is to use annotations. You can check the annotations documentation here.
As a side note, NUnit, reuses the instance of the test, so in the same scenario, it would only instantiate MyTest once.
JUnit will instantiate this class once per test method, so only once in the code above, but try it again with two test methods and you will see it instantiated twice. If you want to save some state in fields without having to use statics, take a look at TestNG, which reuses the same instance for all test methods.