AndroidDriver cant be initialized after mvn clean - selenium

As usually, before running mvn test i executed mvn clean, but this time a lot of things started downloading, once it finished i ran the code and got this error:
class org.openqa.selenium.Platform$22 cannot be cast to class java.lang.String (org.openqa.selenium.Platform$22 is in unnamed module of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap')
I actually didnt change anything, it just stop working (Last time I checked that this worked was 1 week ago)
This is the code that generates the error:
DesiredCapabilitiescap = new DesiredCapabilities();
URL url = new URL("http://0.0.0.0:4723/wd/hub");
cap.setCapability("platformName", "Android");
cap.setCapability("deviceName", "emulator-5554");
cap.setCapability("avd", "Pixel_4_API_30");
cap.setCapability("appPackage", "com.myapp.dev");
cap.setCapability("appActivity", "com.myapp.splash.SplashActivity");
cap.setCapability("appWaitActivity","com.myapp.fulllogin.WelcomeActivity");
cap.setCapability("automationName", "UiAutomator2");
cap.setCapability("noReset", "false");
AppiumDriver driver = new AndroidDriver(url, cap);

There could be version incompatibilities. When I got the same error, I made the following changes and it worked:
In the pom.xml upped the version of java-client to 8.1.0 and added additional dependency selenium-support with compatible version 4.2.0
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>8.1.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-support</artifactId>
<version>4.2.0</version>
</dependency>
Optionally, made changes to the way capabilities are set for platform. Replaced capabilities.setCapability("platformName", "android") with
capabilities.setCapability("appium:platformName", MobilePlatform.ANDROID)
import io.appium.java_client.remote.MobilePlatform;
...
DesiredCapabilities capabilities = new DesiredCapabilities();
...
capabilities.setCapability("appium:platformName", MobilePlatform.ANDROID);

Related

Allure is not generating report on cucumber 7

After updating the cucumber version to 7.2.3, Allure not generating reports. (unknown report and NaN%). It still uses the config file in the directory. I think it's not about the folder path. If I decrease the version to 5.6.0 it is working.
The related part of the pom.xml is like below.
Does anyone have a solution for this??
Thanks,
<dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>7.2.3</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>7.2.3</version>
</dependency>
<dependency>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-cucumber7-jvm</artifactId>
<version>2.17.3</version>
</dependency>
<dependencies>
<argLine>
-Dcucumber.options="--plugin io.qameta.allure.cucumber7jvm.AllureCucumber7Jvm
</argLine>
I know that it doesn't answer the exact question but hope it can help somebody.
I had the same issue with Gradle - build/allure-results folder had only one small json file that generated empty allure report.
It seems like not all the Cucumber and Allure versions are compatible with each other. So I have found a compatible versions pair of Cucumber and Allure. Surprisingly they are the same as author has:
cucumberVersion = '7.2.3'
allureVersion = '2.17.3'
related part of my build.gradle:
plugins {
id 'java'
id 'io.qameta.allure' version '2.9.1'
}
apply plugin: 'io.qameta.allure'
dependencies {
implementation group: 'io.cucumber', name: 'cucumber-java', version: '7.2.3'
testImplementation group: 'io.cucumber', name: 'cucumber-java', version: '7.2.3'
implementation group: 'io.qameta.allure', name: 'allure-cucumber7-jvm', version: '2.17.3'
}
configurations {
cucumberRuntime {
extendsFrom testImplementation
}
}
task cucumber() {
dependsOn assemble, testClasses
doLast {
javaexec {
main = "io.cucumber.core.cli.Main"
classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
args = ['--plugin', 'html:results.html',
'--plugin', 'pretty',
'--plugin', 'io.qameta.allure.cucumber7jvm.AllureCucumber7Jvm',
'--glue', 'step_definitions',
'src/test/resources']
}
}
}
No, no this problem is not depend any cucumber version. Allure report have to write allure-result to any folder on test executing so you can use this commandline under <argLine> raw your pom.xml.
<systemPropertyVariables>
<allure.results.directory>
${project.build.directory}/allure-results
</allure.results.directory>
</systemPropertyVariables>

Intellij not recognizing javax.mail imports but project is building fine

I am using javax.mail library in my project. My project is building fine using - mvn clean install, but when i try to debug my Intellij IDE shows error and it is not able to recognize the javax.mail imports. I have restarted my IDE from FILE -> Invalidate Caches and restart,still no luck.
These are not getting recognized by intellij IDEA,stating unused imports. I am using below the depeendency versions as:- javax.activation - 1.1.1 and javax.mail - 1.4.
Because the project is building fine,i believe the problem lies in some IDE setting.Kindly let me know if this can be resolved.
Try this maven dependency:
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
<scope>provided</scope> <!-- add this only if code will run in a java container (i.e. tomcat, etc)-->
</dependency>
And you should also see the mail classes under External Libraries -> Maven:javax.mail:mail:1.4 -> mail-1.4.jar -> javax.mail
There are also newer versions of the java mail dependency you can use, like 1.4.7 or 1.5.0-b01
The latest version (as pointed out by #Mark Rotteveel) is 1.6.3 and the maven coordinates have changed to jakarta:
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>jakarta.mail</artifactId>
<version>1.6.3</version>
</dependency>
Based on your code I have created a simplified project version with only two files; pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>message-test</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
</dependencies>
</project>
and SendMail.java
package com.test;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Properties;
public class SendMail {
public static void main(String[] args) {
sendMail(new Exception("Problem with cable"));
}
public static void sendMail(Exception exception) {
String to = "destination#test.com";
String from = "sender#test.com";
String host = "smtp.test.com";
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host);
Session session = Session.getDefaultInstance(properties);
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("Trade-processor instance shutdown!");
message.setText(getExceptionMessage(exception));
Transport.send(message);
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
private static String getExceptionMessage(Exception e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
return sw.toString();
}
}
Make sure your 'java' source folder is marked as source (right click on it and select 'Mark directory as -> sources root' if it is not already light blue)
Make sure the class package (com.test) names match i.e. 'src/main/java/com/test/SendMail' on the project pane and 'package com.test' in SendMail.java

Run JUnit Jupiter test with Spring 4.1 and JUnit4 Runner in IntelliJ

Following is my objective:
To write Jupiter test case [Test case in Junit5] for Spring code in version 4.1.6.RELEASE.
I chose to do following [as Junit5 supports running Jupiter tests on Junit4 runner]:
Write a Jupiter test case [Test case in Junit5]
Run with Junit4 Runner [In IntelliJ IDE]
IDE and Code version Details:
Spring Version: 4.1.6.RELEASE
Java Version: 1.8 [Java 8]
IDE: IntelliJ Community 2018.3
Problem i face:
When i run the jupiter test case in IntelliJ IDE:
i see the test case is run by junit5.
[IntelliJ trace shows: "com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit5"]
Resultantly, the intended object is not getting autowired/injected in my test class.
I have added both junit4 and junit5 to classpath from code.
Can someone suggest how to configure intellij to take only junit4 runner everytime i run test.
Class which is to be Tested:
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
#Repository
public class MyClassImpl implements IMyClass {
#PersistenceContext
private EntityManager entityManager;
#Override
public String getName(Long Id) {
String queryString = "some query string";
Query query = entityManager.createNativeQuery(queryString);
List<String> resultList = query.getResultList();
return resultList.isEmpty() ? null : resultList.get(0);
}
Unittest code: TestClass to test MyClassImpl class.
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
#WebAppConfiguration
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes = {EnvironmentConfig.class, DataConfig.class, WebConfig.class})
#ActiveProfiles("dev")
class MyClassImplTest {
#Autowired
private IMyClass myClassObj;
#Test
void getAssessmentNameTestIT()
{
if (myClassObj == null)
System.out.println(" ************ I am NULL ************");
else {
System.out.println(" ************ I am NOT NULL ************");
myClassObj.getName(21L);
}
}
}
Note: Same test case if i write using only Junit 4 [without using any junit5 libraries], the intended object is getting injected.
Maven Dependencies:
junit-jupiter-api RELEASE
junit-jupiter-engine RELEASE
junit-platform-runner 1.2.0
junit 4.10 [test]
Maven Plugin:
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies>
Thanks in advance
#RunWith(SpringJUnit4ClassRunner.class) only works with JUnit 4, hence the "JUnit4" in the class name.
Thus you cannot use annotations from JUnit 5 (JUnit Jupiter) -- such as org.junit.jupiter.api.Test -- if you are using a JUnit 4 Runner. That will never work.
To use Spring's testing support with JUnit Jupiter, you have to use the SpringExtension instead of the SpringJUnit4ClassRunner or SpringRunner -- like this: #ExtendWith(SpringExtension.class).
The SpringExtension is officially supported as part of the spring-test artifact since Spring Framework 5.0: https://docs.spring.io/spring/docs/current/spring-framework-reference/testing.html#testcontext-junit-jupiter-extension
Note that it is possible to use the original version of the SpringExtension with Spring Framework 4.3.x if you use it from my GitHub repository: https://github.com/sbrannen/spring-test-junit5
In your case, it will not be possible to use Spring's testing support with JUnit 5 if you are still on Spring Framework 4.1.6.RELEASE. You will need to upgrade to at least Spring Framework 4.3.x or, preferably, Spring Framework 5.1.x.
Regards,
Sam (author of the Spring TestContext Framework)

Initializaton error in com.cucumber.listener.ExtentCucumberFormatter

I am running the script using Cucumber in BDD Framework and I am using Extent Reports plugin to create the execution report.
I've created the test runner class as below:
package com.ctl.it.qa;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
#RunWith(Cucumber.class)
#CucumberOptions(features = { "src/test/resources/Feature/ABC.feature"
},
plugin = {"com.cucumber.listener.ExtentCucumberFormatter:BDDControlCenterTools/target/Reports/cucumber-report.html"}
)
public class RunCukes {
}
I have included the below dependency for the Extent report in the POM.xml file:
<dependency>
<groupId>com.relevantcodes</groupId>
<artifactId>extentreports</artifactId>
<version>2.41.2</version>
</dependency>
I am running the script with Junit and have the cucumber dependency for Junit too.
But when I execute the above runner class, its showing an Initialization error:
cucumber.runtime.CucumberException: Couldn't load plugin class: com.cucumber.listener.ExtentCucumberFormatter
Can anyone please help in this error and help to resolve it.
You need to also add the Maven dependency for this formatter. Refer to this -- https://github.com/email2vimalraj/CucumberExtentReporter documents.
<dependency>
<groupId>com.vimalselvam</groupId>
<artifactId>cucumber-extentsreport</artifactId>
<version>2.0.5</version>
</dependency>
But i think this only works with ExtentReport version 3 and above.
I was having com.cucumber.listener.ExtentCucumberFormatter initialization error but after few tweaks. I can generate the report now.
I added these two to my POM file. The version can be tricky as I used 3.1.1 for cucumber-extentreport but it didn't work for me. After trying a few 3.0.2 worked.
<dependency>
<groupId>com.vimalselvam</groupId>
<artifactId>cucumber-extentsreport</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>com.aventstack</groupId>
<artifactId>extentreports</artifactId>
<version>3.1.1</version>
</dependency>
My runner class looked like this:
package cucumber;
import java.io.File;
import org.junit.AfterClass;
import org.junit.runner.RunWith;
import com.cucumber.listener.Reporter;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
#RunWith(Cucumber.class)
#CucumberOptions(
features = {"src/test/resources/features",
glue = {"stepDefinitions"},
plugin = {"com.cucumber.listener.ExtentCucumberFormatter:target/cucumber-reports/report.html"},
monochrome = true
)
public class CucumberRunner {
#AfterClass
public static void writeExtentReport() {
Reporter.loadXMLConfig(new File("config/report.xml"));
}
}
I hope this helps.
Try using a different version of cucumber-extentsreport. For me, the latest version (3.1.1) did not work, but 3.0.2 did.
To resolve this.
Remove below code "com.cucumber.listener.ExtentCucumberFormatter:target/report.html" from the runner class and then run the runner class.
It will run successfuly. Then put this back into the runer class and execute, it will work.

What's the difference between jython-standalone-2.7.0.jar and jython-2.7.0.jar

I wrote a Java example, the code is:
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.util.List;
class JythonExample {
public static void main(String args[]) throws ScriptException {
listEngines();
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine pyEngine = mgr.getEngineByName("python");
try {
pyEngine.eval("print \"Python - Hello, world!\"");
} catch (Exception ex) {
ex.printStackTrace();
}
final PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("print \"Python - Hello, world!\"");
PyObject result = interpreter.eval("2 + 3");
System.out.println(result.toString());
}
public static void listEngines(){
ScriptEngineManager mgr = new ScriptEngineManager();
List<ScriptEngineFactory> factories =
mgr.getEngineFactories();
for (ScriptEngineFactory factory: factories) {
System.out.println("ScriptEngineFactory Info");
String engName = factory.getEngineName();
String engVersion = factory.getEngineVersion();
String langName = factory.getLanguageName();
String langVersion = factory.getLanguageVersion();
System.out.printf("\tScript Engine: %s (%s)\n",
engName, engVersion);
List<String> engNames = factory.getNames();
for(String name: engNames) {
System.out.printf("\tEngine Alias: %s\n", name);
}
System.out.printf("\tLanguage: %s (%s)\n",
langName, langVersion);
}
}
}
In my pom.xml, if I use:
<dependency>
<groupId>org.python</groupId>
<artifactId>jython-standalone</artifactId>
<version>2.7.0</version>
</dependency>
then I can run java -jar target/jython-example-1.0-SNAPSHOT.jar successfuly, by the way, I used maven-assembly-plugin to build a runnable jar.
if I use:
<dependency>
<groupId>org.python</groupId>
<artifactId>jython</artifactId>
<version>2.7.0</version>
</dependency>
then when I run java -jar target/jython-example-1.0-SNAPSHOT.jar, I'll always get the following error:
ScriptEngineFactory Info
Script Engine: jython (2.7.0)
Engine Alias: python
Engine Alias: jython
Language: python (2.7)
ScriptEngineFactory Info
Script Engine: Oracle Nashorn (1.8.0_31)
Engine Alias: nashorn
Engine Alias: Nashorn
Engine Alias: js
Engine Alias: JS
Engine Alias: JavaScript
Engine Alias: javascript
Engine Alias: ECMAScript
Engine Alias: ecmascript
Language: ECMAScript (ECMA - 262 Edition 5.1)
java.lang.NullPointerException
at me.soulmachine.JythonExample.main(JythonExample.java:21)
Exception in thread "main" ImportError: Cannot import site module and its dependencies: No module named site
Determine if the following attributes are correct:
* sys.path: ['/home/programmer/src/github/JythonExample/JythonExample/target/Lib', '__classpath__', '__pyclasspath__/']
This attribute might be including the wrong directories, such as from CPython
* sys.prefix: /home/programmer/src/github/JythonExample/JythonExample/target
This attribute is set by the system property python.home, although it can
be often automatically determined by the location of the Jython jar file
You can use the -S option or python.import.site=false to not import the site module
It seems the pyEngine is null.
So I wonder what's the difference between jython-standalone-2.7.0.jar and jython-2.7.0.jar
One problem I've just discovered with the same error is that the maven build 2.7.0 does not include the lib folder. This is probably a build error for the release build. I had to move up the b2 build which does properly include the lib folder in the supplied jar.
Problem maven 2.7.0 jar:
<dependency>
<groupId>org.python</groupId>
<artifactId>jython-standalone</artifactId>
<version>2.7.0</version>
</dependency>
Working maven 2.7.1b2 that includes the lib folder:
<dependency>
<groupId>org.python</groupId>
<artifactId>jython-standalone</artifactId>
<version>2.7.1b2</version>
</dependency>
NOTE: If you download the jar directly from the Jython site it does correctly include the lib folder. It's just the maven repository version.
I believe the main difference causing your issue is that the jython-standalone jar provides Lib/ (which contains site.py) while the jython jar does not.
https://github.com/scijava/jython-shaded gives a more in-depth description of the issue, as well as other issues, and provides an alternative jar to get around some issues noted in the description.
I don't have experience with scijava:jython-shaded, but I substituted it into your pom (for my setup I also had to change jdk.version to 1.7 and to JythonExample) and your example runs.