How to export a TestNG suite to jar file - selenium-webdriver

I'm new to Selenium I have exported TestNG suite to jar file using the Eclipse export option but am unable to run it.

Inorder to run a jar file you need to have a class with main method pointed to your Launch configuration while exporting.
you can run TestNG programmatically by creating this main method,
public static void main(String[] args) {
TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
testng.setTestClasses(new Class[] { Run2.class });
testng.addListener(tla);
testng.run();
}
Add this main method class to your Launch Configuration while exporting the jar in eclipse.
Refer this testNG doc for more information : Run testNG programmatically
If you are using maven-jar-plugin to export jar make sure you have added your mainClass in manifest.

Related

Cucumber test failed : Exception in thread "main" java.lang.NoClassDefFoundError: io/cucumber/plugin/SummaryPrinter

I was trying to execute basic test from Cucumber feature file but found the error:
Exception in thread "main" java.lang.NoClassDefFoundError: io/cucumber/plugin/SummaryPrinter
Feature File:
Feature: Login Action
Scenario: Successful Login with Valid Credentials
Given User is on Home Page
When User Navigate to LogIn Page
And User enters UserName and Password
Then Message displayed Login Successfully
Scenario: Successful LogOut
When User LogOut from the Application
Then Message displayed LogOut Successfully
Test Runner File:
package test;
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
#RunWith(Cucumber.class)
#CucumberOptions(
features = "Feature"
,glue={"stepdefinition"}
)
public class TestRunner {
}
I am using eclipse and have installed cucumber plugin through eclipse marketplace.
Can anyone help me to fix this issue?
package cucumberOptions;
import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;
//#RunWith(Cucumber.class)
#CucumberOptions(
features = "src/test/java/features",
glue = "stepDefinations")
public class TestRunner extends AbstractTestNGCucumberTests {
}
Just keep in mind that features, stepDefinations, are folders at the same level, also my testRunner class is inside a folder called 'cucumberOptions' which is at the same level than the other 2 folders, in my case they are inside test folder, since this is a maven like project
It looks that missing dependency of one of the jar. It's really bean hectic and cumbersome with new versions of cucumber as there are always coming with breaking changes. I also seen same error while upgrading from cucumber 5.1.3 to 5.7.0. After spending sometime i resolved it by adding below additional dependency in pom:
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-plugin</artifactId>
<version>5.7.0</version>
</dependency>

Integrate test Selenium with VSTS

I made a set of tests of IU with Selenium, creating for it a Class Library, when integrating it with VSTS, what should I do so that the tests run when I build it?
If it is the .net test project, you can refer to this article: Get started with Selenium testing in a CI pipeline
Simple steps:
Create a Unit Test project
Add Selenium.WebDriver, Selenium.WebDriver.ChromeDriver, Selenium.WebDriver.IEDriver, Selenium.Firefox.WebDriver, Selenium.WebDriver.PhantomJS.Xplatform packages to your project
Coding
Sample:
namespace Partsunlimited.UITests
{
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.PhantomJS;
using System;
[TestClass]
public class ChucksClass1
{
private string baseURL = "http://your-website.azurewebsites.net/";
private RemoteWebDriver driver;
private string browser;
public TestContext TestContext { get; set; }
[TestMethod]
[TestCategory("Selenium")]
[Priority(1)]
[Owner("FireFox")]
public void TireSearch_Any()
{
driver = new FirefoxDriver();
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(30));
driver.Navigate().GoToUrl(this.baseURL);
driver.FindElementById("search - box").Clear();
driver.FindElementById("search - box").SendKeys("tire");
//do other Selenium things here!
}
[TestCleanup()]
public void MyTestCleanup()
{
driver.Quit();
}
[TestInitialize()]
public void MyTestInitialize()
{
}
}
}
Create new build definition
Add NuGet task to restore packages
Add Visual Studio Build task to build projects
Add Visual Studio Test Agent Deployment task to deploy test agent
Add Windows Machine File Copy to copy files to agent machine
Add Run Functional Tests task to run test
You also can use Visual Studio Test task to run test (Replace step 6 ~9), a blog: Running Selenium Tests as part of your release with VSTS Release Management

Flink Cannot load user class on running from IDE

I have a Flink application and generating a fat jar with all its dependencies I can submit it successfully on the remote cluster, using flink run command. Trying to submit the application on IntelliJ IDE, I faced the folloing error:
Caused by: org.apache.flink.streaming.runtime.tasks.StreamTaskException: Cannot load user class: MyFlink$1
ClassLoader info: URL ClassLoader:
file: '/tmp/blobStore-0fe289f8-b35b-4666-8402-67f9f6a22f55/cache/blob_3fd776b533f2268b6cf7ef1cc62b187bc4513c99' (valid JAR)
Class not resolvable through given classloader.
I packaged dependencies in a single jar file and pass it in createRemoteEnvironment method.
StreamExecutionEnvironment env = StreamExecutionEnvironment.createRemoteEnvironment(
"jobmanager", 6123, 2,
"FlinkProcessing_dependencies.jar"
);
How can I disaper this error?
Notice: Passing all user-defined classes in addition to dependencies, it runs successfully, but I don't want to pass the classes jars and export a jar file each time I change my classes!

How do I set up Allure with JUnit in an IntelliJ project?

I've been reading in the Allure wiki however I do not seem to be able to get started with Allure.
I have an IntelliJ project which I use to run JUnit tests with Selenium. I want to add Allure for better feedback after test runs. However I've not been able to understand how Allure would integrate with the rest of my project.
On the wiki page for JUnit it looks like Allure with JUnit only supports maven projects? How can I set up allure to work with an IntelliJ project?
I want to add Allure for better feedback after test runs
It is strange that you don't have a build tool.
But for single test (as you mention) following will work.
Dependencies - you need aither allure-junit-adaptor or allure-testng-adaptor
Allure implements test listener, which should be added to test runner:
For TestNG it happens automatically (once you add adaptor dependency).
For JUnit you should add listener manually. I don't know how to add it to Intellij Idea JUnit runner, but you can always run tests programmatically:
public static void main(String[] args) {
JUnitCore runner = new JUnitCore();
runner.addListener(new AllureRunListener());
runner.run(CalculatorTest.class);
}
That will generate XML report in target/allure-results folder.
If you need advanced Allure features like file attachments and test steps you need another dependency (aspectjweaver) and according JVM args, e.g.
-javaagent:lib/aspectjweaver-1.8.7.jar
To generate HTML report from existing XML report you can:
either use Allure CLI (requires tool installation http://wiki.qatools.ru/display/AL/Allure+Commandline)
or use 'mvn site' on existing project (e.g. https://github.com/allure-examples/allure-junit-example)
Open your HTML report in Firefox (or look here how to open locally generated report in Chrome).
To have allure-results after launching JUnit4 tests from IDE using context menu or controls on the gutter I use #RunWith anntotation where I set my custom runner with AllureJUnit4 listener.
CustomRunner class
package ru.atconsulting.qa.system.junit4runners;
import io.qameta.allure.junit4.AllureJunit4;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.InitializationError;
public class CustomRunner extends BlockJUnit4ClassRunner {
/**
* Creates a BlockJUnit4ClassRunner to run {#code klass}
*
* #param klass
* #throws InitializationError if the test class is malformed.
*/
public CustomRunner(Class<?> klass) throws InitializationError {
super(klass);
}
#Override
public void run(RunNotifier notifier) {
notifier.addListener(new AllureJunit4()); //just add listener
super.run(notifier);
}
}
Using #RunWith annotation in test classes
#RunWith(CustomRunner.class)
public class ExampleSuite {
#Test
public void testExample() {
....
}
}

How to import existing Java.class Files to Android Studio Project

I need to know how I can properly add existing Java.class files to an Android Studio Project. My goal is to use these classes in an Android Project.
The Class Files are already written in Eclipse for another Java Project.
I've already tried File->New->New Module->selecting Java Library->Finish but that doesn't work properly.
As you probably all know it makes the MyClass Class by default.
For testing I imported com.example.* in my MainActivity and tried to build an Object of that Class inside the onCreate() Method.
The problem is it can't compile the Project. I got the following Errors:
Error:(7, 1) error: package com.example does not exist
Error:(16, 9) error: cannot find symbol class MyClass
Note: C:\Users\...\MainActivity.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Error:Execution failed for task ':app:compileDebugJava'.
> Compilation failed; see the compiler error output for details.
Can anybody explain how to import my Java.class files correctly so that I can use them in my project?
You can add as a local library package to your project in Android Studio.
In android Project window, right click on app and select New -> Module.
In the Create New Module window, select Java Library and click next. Then give the module name, for example HttpClient in Library Name field. Then give Java package name as same as your existing package com.example.xxx. Then give one existing class file name AnyFileName in Java Class Name field.
Now new module is created with the name HttpClient and the package name is com.example.xxx. With an empty class file Anyfilename.java
Now copy all your .java files to the HttpClient folder created inside your android project. Now it would have overwritten the empty file Anyfilename.java also.
After copying all .java files would have automatically added to the library module.
And you will get 3rd build.gradle file for your module. Already you might be having 2 build.gradle for your android project.
In your app's build.gradle file, include local library dependency compile project(":HttpClient"). Now you can import java files in HttpClient module to android app's java files.
Note : Above information is given based on Android Studio 2.3.3

Resources