How use jmockit without junit - jmockit

How use jmockit without junit? not use annotation
run main:
/**
*
* #param args
*/
public static void main(String[] args) {
try {
JMockit jMockit = new JMockit(JmockitTest.class);
// to do
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

You can use the MockUp API outside of tests, but not the Expectations API; the latter can only be used from JUnit or TestNG tests.

Related

Integrate Extent Reports with AWS Device Farm and Jenkins

I am new to automation and I have been integrating AWS device farm to run my test cases on cloud. I have integrated Jenkins with AWS device farm to run the tests on the go. I want to integrate Extent Reports to see the results of the run inside Jenkins. I can't find any tutorial to do so. Can you please help me with this.
I have installed the HTML publisher in Jenkins and I have implemented Extent Reports for my local run and its working. But, I have no idea how to integrate for the cloud.
Thanks in advance. Stay Safe
Here is my code for local integration of Extent Reports
ExtentTest test;
ExtentReports extent = ExtentReportsBlackstone.getReportObject();
ThreadLocal<ExtentTest> extentTest = new ThreadLocal<ExtentTest>();
AppiumDriver<?> driver ;
#Override
public void onTestStart(ITestResult result) {
test = extent.createTest(result.getMethod().getMethodName()).assignCategory(result.getMethod().getGroups());
extentTest.set(test);
}
#Override
public void onTestSuccess(ITestResult result){
// TODO Auto-generated method stub
extentTest.get().log(Status.PASS, "Test Passed");
Properties prop = UtilityBase.globalProperties();
if(prop.getProperty("captureScreenshotOnTestPass").equals("true")) {
String testMethodName = result.getMethod().getMethodName();
try {
Class clazz = result.getTestClass().getRealClass();
Field field = clazz.getDeclaredField("driver");
field.setAccessible(true);
driver = (AppiumDriver<?>) field.get(result.getInstance());
} catch(Exception e) {
e.printStackTrace();
}
try {
extentTest.get().addScreenCaptureFromPath(getScreenshot(this.driver,testMethodName), result.getMethod().getMethodName());
} catch(IOException e){
e.printStackTrace();
}
}
}
#Override
public void onTestFailure(ITestResult result) {
// TODO Auto-generated method stub
extentTest.get().fail(result.getThrowable());
String testMethodName = result.getMethod().getMethodName();
try {
Class clazz = result.getTestClass().getRealClass();
Field field = clazz.getDeclaredField("driver");
field.setAccessible(true);
driver = (AppiumDriver<?>) field.get(result.getInstance());
} catch(Exception e) {
e.printStackTrace();
}
try {
extentTest.get().addScreenCaptureFromPath(getScreenshot(this.driver,testMethodName), result.getMethod().getMethodName());
} catch(IOException e){
e.printStackTrace();
}
}
#Override
public void onTestSkipped(ITestResult arg0) {
// TODO Auto-generated method stub
}
#Override
public void onTestFailedButWithinSuccessPercentage(ITestResult arg0) {
// TODO Auto-generated method stub
}
#Override
public void onStart(ITestContext context) {
// TODO Auto-generated method stub
try {
UtilityBase.deleteFolder(System.getProperty("user.dir")+"/reports");
}catch(IOException e) {
e.printStackTrace();
}
}
#Override
public void onFinish(ITestContext context) {
// TODO Auto-generated method stub
extent.flush();
}
}
estimate cannot be integrated, aws is a bit limited and for that option we opted for lambatest, where we send it to run in the cloud but the evidence remains in extend report in our aws node
aws asks to compile the project and upload the project which runs on its own server, I don't like that way of execution

Quarkus ExceptionMapper does not handle WebApplicationException

I'm trying to understand if this is a feature or a bug... :-)
For the below controller and exception mapper, for a rest client that will fail with a 401 response, I would expect the exception handler to be invoked for both cases. However it's not invoked for the WebApplicationException. Why is that and how are you meant to register an exception handler for them cases. This is using Quarkus version 0.21.2
#Path("/failable")
public class FailableResource {
#Inject
#RestClient
private SomeHttpClient httpClient;
#GET
#Path("fails")
#Produces(MediaType.TEXT_PLAIN)
public String fails() {
try {
return httpClient.someQuery();
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
#GET
#Path("works")
#Produces(MediaType.TEXT_PLAIN)
public String works() {
try {
return httpClient.someQuery();
} catch (Exception e) {
e.printStackTrace();
throw new IllegalStateException("Not a WebApplicationException");
}
}
}
And the ExceptionMapper
#Provider
public class HandleMySillyError implements ExceptionMapper<Throwable> {
#Override
public Response toResponse(Throwable e) {
e.printStackTrace();
return Response.ok("Some handled response").build();
}
}
I found out when running in quarkus:dev mode the exception mapper is not invoked. It seems that this is caused by an exception mapper from quarkus that is only used in DEV mode (see https://github.com/quarkusio/quarkus/issues/7883).
I launched my code local as normal a normal java program, causing my exception handler to work as expected. Also when running the code on Openshift, my custom exception mapper is used as well.
note: I used quarkus version 1.8.3

Dealing with multiple JUnit test cases

I'm writing JUnit test cases using the selenium web driver, and I'm running into issues when trying to make multiple tests in one file. Here is an example of my code structure:
public class exampleScripts extends SeleneseTestCase {
public void setUp() throws Exception{
SeleniumServer seleniumServer = new SeleniumServer();
seleniumServer.start();
setUp("https://my.target.URL", *firefox");
}
#Test
public void test1() throws Exception {
//do test stuff
try {
//check if results are good
}
catch (Throwable e) {
//handle error
}
}
#Test
public void test2() throws Exception {
//do more test stuff
try {
//check results
}
catch (Throwable e) {
//handle error
}
}
}
Now, the tests by themselves work correctly, but I am getting the error Failed to start: SocketListener1#0.0.0.0:4444 when running my class as a whole. The first test runs correctly, but the second I move on to the next test, it seems like it's trying to re-start the session, when I just want it to continue on the old session. How do I get around this issue?
It seems I was able to find a wonky work-around for this problem, though I wouldn't recommend this to others as a solution. I created a test_suite function to run all my tests, and log the results myself.
public void setUp() {
//setUp stuff
}
public void testAll() throws Exception {
selenium.open("my/target/path");
test1();
test2();
}
#Test
public void test1() throws Exception {
//test stuff
try {
//check results
}
catch (Throwable e) {
log.INFO("There was an error in test1");
}
}
//and so on
The whole test suite would be run on testAll(). Again, would not recommend this solution, as it does not take advantage of JUnit's logging, but it worked for me.

Screen resolution test

Does anyone know a desktop app for testing websites in different screen resolutions? The reason I am looking for a desktop app is because I want to be able to test websites that have internal URLs [of course I want to test in resolutions that are not available on my computer).
Thanks
I had similar problem. You can write JUnit and Selenium test app for that. It's not big deal, simply change screen resolution of driver object and take print screen automaticly. Image files are store in your project so you can see after test all resolutions of your site. Here is code for that :
public class TestCase1 {
/**
*
*/
private WebDriver driver;
private String url;
public TestCase1() {
}
#BeforeClass
public static void setUpClass() {
}
#AfterClass
public static void tearDownClass() {
}
#Before
public void setUp() {
// array of test urls
this.url = "http://www.google.com/";
// ChromeDriver
//System.setProperty("webdriver.chrome.driver", PATH_TO_CHROMEDRIVER);
//driver = new ChromeDriver();
driver = new FirefoxDriver();
driver.get(this.url);
}
#After
public void tearDown() {
driver.quit();
}
#Test
public void testResolution() {
driver.navigate().to(this.url);
String[] resulutions = {
"1366x768",
"1920x1080"
// add resolution
};
for (String resulution : resulutions) {
String[] parts = resulution.split("x");
// Screen resolution
Dimension screenRes = new Dimension(Integer.parseInt(parts[0]),Integer.parseInt(parts[1]));
// Set browser resolution
driver.manage().window().setSize(screenRes);
// little pause
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
//Logger.getLogger(TestClass.class.getName()).log(Level.SEVERE, null, ex);
}
driver.navigate().refresh();
// little pause
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
//Logger.getLogger(TestClass.class.getName()).log(Level.SEVERE, null, ex);
}
this.takeScreenShot(resulution);
}
}
/**
*
* #param fileName
*/
private void takeScreenShot(String fileName) {
File screenShot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(screenShot, new File(fileName + ".png"));
} catch (IOException ioe) {
throw new RuntimeException(ioe.getMessage(), ioe);
}
}
}
Here is link for setting test project, importing selenium and other staff that you need to do that.

Capture WebDriver Screenshots When Running Parallel Tests With TestNG

I am currently capturing screenshots on failure and success in TestNG by way of overriding the TestListenerAdapter methods onTestFailure, and onTestSuccess respectively. In order to do this you need to specify which driver you want to take a screenshot of.
My question: Is there a good way to capture screenshots when running tests in parallel on the method level?
In order to run tests in parallel, each individual test needs a unique driver instance. So, at any given time you have x number of driver instances running. When it comes time to capture a screenshot, how do you determine which driver to use?
Code excerpts below:
public class OnFailureListener extends TestListenerAdapter {
#Override
public void onTestFailure(ITestResult tr) {
Screenshots.captureScreenshot(tr);
super.onTestFailure(tr);
}
--
public static void captureScreenshot(ITestResult tr) {
WebDriver driver = TestClass.driver;
if (driver instanceof TakesScreenshot) {
String filename = "path/to/screenshot/file";
try {
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File(filename));
} catch (IOException e) { e.printStackTrace(); }
}
If you create a base test class with access to the driver, then that driver will always be the correct driver
The following will achieve this;
All test classes must extend a simple base test class;
public asbtract baseTestCase() {
private WebDriver driver;
public WebDriver getDriver() {
return driver;
}
#BeforeMethod
public void createDriver() {
driver=XXXXDriver();
}
#AfterMethod
public void tearDownDriver() {
if (driver != null){
try{
driver.quit();
}
catch (WebDriverException e) {
System.out.println("***** CAUGHT EXCEPTION IN DRIVER TEARDOWN *****");
System.out.println(e);
}
}
}
In your listener, you need to access the base class:
public class ScreenshotListener extends TestListenerAdapter {
#Override
public void onTestFailure(ITestResult result){
Object currentClass = result.getInstance();
WebDriver webDriver = ((BaseTest) currentClass).getDriver();
if (webDriver != null){
File f = ((TakesScreenshot)webDriver).getScreenshotAs(OutputType.FILE);
//etc.
}
}
}