Unitils EasyMock and JUnit #Rule from JUnit 4.10 to 4.11 - easymock

I have the Problem that when I use the #RuleAnnotation from JUnit for my TemporaryFolder and want to use Mocks from unitils.easymock at the same time I get an IlleagalStateException in JUnit 4.11, whereas in JUnit 4.10 it still works.
So the following test runs under JUnit 4.10 and throws the IllegalStateException in 4.11:
import java.io.File;
import java.io.IOException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.unitils.UnitilsJUnit4;
public class MyTest extends UnitilsJUnit4 {
#Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
#Test
public void testSomething() throws IOException {
File newFile = temporaryFolder.newFile();
}
}
Even if I use the Annotation for the Mocking capability instead of extends UnitilsJUnit4 it doesn't work in JUnit 4.11:
#RunWith(UnitilsJUnit4TestClassRunner.class)
public class MyTest {
...
}
The error message when testing this code is:
java.lang.IllegalStateException: the temporary folder has not yet been created
Something new I just additionally found out: In JUnit 4.10 I can also enforce the same error when passing a String in the newFile() call:
File newFile = temporaryFolder.newFile("");
My question:
What is the proper way to make TemporaryFolders or #Rules in general work together with unitils.easymock.annotation.Mocks in JUnit 4.11?
Or is Mocking with the easymock #Mock annotation and #Rules at the same time simply not possible?
Versions:
easymock 3.4
unitils 3.4.3

Related

JSR233 sampler with Java to work with Selenium Webdriver (javax.script.ScriptException: In file: inline evaluation)

Trying to run Selenium Webdriver script in Jmeter using JSR233 sampler. The script works fine in Eclipse IDE, however in Jmeter facing below error.
ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script JSR223 Sampler,
message: javax.script.ScriptException: In file: inline evaluation of:
``import java.util.HashMap; import org.openqa.selenium.WebDriver; import
org.openq . . . '' Encountered "," at line 28, column 25.
in inline evaluation of: ``import java.util.HashMap; import
org.openqa.selenium.WebDriver; import org.openq . . . '' at line number 28
javax.script.ScriptException: In file: inline evaluation of: ``import
java.util.HashMap; import org.openqa.selenium.WebDriver; import org.openq .
. . '' Encountered "," at line 28, column 25.
in inline evaluation of: ``import java.util.HashMap; import
org.openqa.selenium.WebDriver; import org.openq . . . '' at line number 28
at bsh.engine.BshScriptEngine.evalSource(BshScriptEngine.java:82) ~[bsh-
2.0b6.jar:2.0b6 2016-02-05 05:16:19]
at bsh.engine.BshScriptEngine.eval(BshScriptEngine.java:46) ~[bsh-
2.0b6.jar:2.0b6 2016-02-05 05:16:19]
at javax.script.AbstractScriptEngine.eval(Unknown Source) ~[?:1.8.0_181]
Below is the script trying to execute:
import java.util.HashMap;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium;
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
String downloadFilepath = "D:/MyDeskDownload";
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
// chromePrefs.put("profile.default_content_settings.popups", 0);
// chromePrefs.put("download.default_directory", downloadFilepath);
// chromePrefs.put("safebrowsing.enabled", "true");
ChromeOptions options1 = new ChromeOptions();
options1.setExperimentalOption("prefs", chromePrefs);
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options1);
WebDriver driver = new ChromeDriver(cap);
driver.setJavaScriptEnabled(true);
driver.get("http://google.com/");
I have gone through below references to get above script:
Running Selenium scripts with JMeter
How to use WDS variable in BSF or JSR233 (JMeter)
We could achieve launching a browser and performing actions with Selenium Webdriver config sampler with JavaScript however since we are unable to set capabilities with WDS, we are trying to achieve the same in JSR233.
From stacktrace it looks you are using JSR223 with Beanshell or Java (which will be beanshell).
Since it's Beanshell, it doesn't understands generics (diamond operator) so this line:
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
So you need to just switch the language to Groovy to fix the problem:
Beanshell doesn't support the diamond operator to if you really want to continue wiht Beanshell - change this line:
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
to this one
HashMap chromePrefs = new HashMap();
Be aware that starting from JMeter version 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting, the reasons are in:
Groovy supports all the modern Java language features
Groovy provides a lot of enhancements over standard Java SDK
Groovy provides much better performance than Beanshell
So consider migrating to Groovy, my expectation is that no changes will be required (you might need rewriting lambdas into closures if any, however the overhead will be minimal)

Chrome extension is not loaded by Selenium in Java

I want to load a chrome extension named scrapbook using Java. I searched a lot and tried different proposed methods to do so. However, none of those are working. Even it does not load the window maximized, let alone loading the extension. Here is the code:
package automationFramework;
import java.io.File;
import com.google.common.base.Function;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
public class FirstTestCase {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "F:\\SOFTWARE\\CHROME EXTENSIONS\\chromedriver_228.exe");
String pathToExtension = "C:\\Users\\user1\\AppData\\Local\\Google\Chrome\\User Data\\Profile 1\\oegnpmiddfljlloiklpkeelagaeejfai\\0.26.3_0\\";
ChromeOptions options = new ChromeOptions();
options.addArguments("-load-extension=" + pathToExtension);
options.addArguments("-open-maximized");
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.google.com");
}
}
I had the same issue, then I used the .crx file to add it on launch. I'm using python, but in Java it would be the same by using:
options.add_extension("idmgcext.crx");
the .crx file is in the same folder as the script or provide full path if in another folder you can get chrome extension for .crx file.

No qualifying bean of type 'org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder' available

In Java project, I am using Sprig Boot 1.5.3.RELEASE. It is connecting with two databases i.e. MongoDB and Microsoft SQLServer. When I run it with spring-boot:run goal, it works fine. However, when I try to run it with package goal then below error is reported by test cases despite the fact that those test cases are not connecting to SQL Server database:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1486)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:467)
.....
.....
MediationTest.java (Java class containing test cases generating above error)
#RunWith(SpringRunner.class)
#DataMongoTest(excludeAutoConfiguration = EmbeddedMongoAutoConfiguration.class)
#SpringBootTest(classes = { Application.class })
public class MediationTest {
#Autowired
private SwiftFormat swiftFormat;
......................
......................
MsqlDbConfig.java
#Configuration
#EnableTransactionManagement
#EnableJpaRepositories(entityManagerFactoryRef = "msqlEntityManagerFactory", transactionManagerRef = "msqlTransactionManager", basePackages = { "com.msql.data" })
public class MsqlDbConfig {
#Bean(name = "msqlDataSource")
#ConfigurationProperties(prefix = "msql.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
#Bean(name = "msqlEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean msqlEntityManagerFactory(
EntityManagerFactoryBuilder builder,
#Qualifier("msqlDataSource") DataSource dataSource) {
return builder.dataSource(dataSource)
.packages("com.utils.msql.info")
.persistenceUnit("msql").build();
}
#Bean(name = "msqlTransactionManager")
public PlatformTransactionManager msqlTransactionManager(
#Qualifier("msqlEntityManagerFactory") EntityManagerFactory msqlEntityManagerFactory) {
return new JpaTransactionManager(msqlEntityManagerFactory);
}
}
application.properties
spring.data.mongodb.uri=mongodb://dev-abc-123:27017/db
msql.datasource.url=jdbc:sqlserver://ABC-SQL14-WXX;databaseName=dev
msql.datasource.username=dev
msql.datasource.password=*****
msql.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
msql.jpa.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy
spring.jpa.show-sql=true
The spring-boot:run goal is defined by the Mojo included within the spring-boot-maven-plugin project. You can find it here. https://github.com/spring-projects/spring-boot/blob/8e3baf3130220a331d540cb07e1aca263b721b38/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RunMojo.java.
The requiresDependencyResolution scope is set to Test. This will include the dependencies from each phase on the classpath. Take a look at the specification here. https://maven.apache.org/developers/mojo-api-specification.html
The package goal provided by Maven wouldn't include these additional dependencies on the classpath and I believe that is the cause of your issues.
Spring Boot provides a repackage goal which is what should be used for building out executable spring-boot applications.
However, to get more to the point. I think if you update your test to exclude an additional class it might fix your problem.
#DataMongoTest(excludeAutoConfiguration = {EmbeddedMongoAutoConfiguration.class, HibernateJpaAutoConfiguration.class})

FAILED: invokeApp org.openqa.selenium.SessionNotCreatedException: A new session could not be created

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;
import io.appium.java_client.android.AndroidDriver;
public class Demo {
AndroidDriver driver =null;
DesiredCapabilities capabilities;
File app = new File("/data/app/com.philips.sleepmapper.root-1/base.apk");
#Test
public void invokeApp() throws MalformedURLException
{
capabilities = new DesiredCapabilities();
capabilities.setCapability("automationName", "Appium");
capabilities.setCapability("paltformName", "Android");
capabilities.setCapability("platformVersion", "6.0.1");
capabilities.setCapability("deviceNmae", "Galaxy S6");
capabilities.setCapability("app", app.getAbsolutePath());
capabilities.setCapability("appPackage","com.philips.sleepmapper.root");
capabilities.setCapability("appactivity","com.philips.sleepmapper.activity.SplashScreenActivity");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
}
When executing this code i am getting the following error:
FAILED: invokeApp org.openqa.selenium.SessionNotCreatedException: A
new session could not be created. (Original error: Bad app:
C:\data\app\com.philips.sleepmapper.root-1\base.apk. App paths need to
be absolute, or relative to the appium server.
The path to your application APK is set incorrectly. I need to know your file structure to give the exact answer, but this is what I think is wrong.
Most likely you are trying to provide the application at C:\path\to\my\project\data\app\com.philips.sleepmapper.root-1\base.apk
If you run Appium in C:\path\to\my\project and you try to pass the relative path to the APK, you are missing the dot in the Appium test code. Change the path in the code to
File app = new File("./data/app/com.philips.sleepmapper.root-1/base.apk");
To make it work from any folder (absolute path) change the code to
File app = new File("C:\path\to\my\project\data\app\com.philips.sleepmapper.root-1\base.apk");
Remember to replace path\to\my\project with the real path you are using.

Selenium how to open a web browser to run a selenium script

I exported a script from selenium IDE 1.9.0 as Java/TestNG/RemoteControl.
I would like to run this script using TestNG within Eclipse and I want to see the script play back in Firefox browser.
I did some search online, but I could not make it work. I need some instructions and guidance on how to make the code work. Your help is greatly appreciated.
Here is my code:
import java.util.List;
import org.testng.annotations.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import static org.testng.Assert.*;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
public class search_donor_suzy_ng //extends SeleneseTestNgHelper {
#BeforeTest
public void setUp() throws Exception {
//initizlize Firefoxbrowser:
WebDriver ffoxdriver = new FirefoxDriver();
String baseUrl = "www.google.com"; //sample URL
}
#Test
public void testSearch_donor_suzy_ng() throws Exception {
// set overall speed of the test case
selenium.setSpeed("4000");
selenium.open("/?html=openid");
selenium.click("css=input[type=\"submit\"]");
selenium.waitForPageToLoad("30000");
Thread.sleep(4000);
selenium.type("id=edit-name", "jeffshu");
selenium.type("id=edit-pass", "tEgvz9xsaNjnwe4Y");
selenium.click("id=edit-submit");
selenium.waitForPageToLoad("30000");
Thread.sleep(4000);
selenium.click("id=cmp_admin");
selenium.waitForPageToLoad("30000");
selenium.click("id=quicksearch_anchor");
selenium.click("css=img[alt=\"Member\"]");
selenium.waitForPageToLoad("30000");
selenium.type("id=search_name", "suzy");
selenium.click("css=input[type=\"image\"]");
selenium.click("link=Balagia, Suzy");
selenium.waitForPageToLoad("30000");
}
#AfterTest
public void tearDown() throws Exception {
ffoxdriver.quit();
}
}
For starters, you do need to refer to the documentation # http://docs.seleniumhq.org/docs/03_webdriver.jsp
In your code, you are intializing the driver object in your beforetest method, which you are not using. Driver is Webdriver's way of launching your browser, whereas in your testmethod you are using selenium and selenium 1 commands. As a quick step, you can replace your selenium with driver (put your declaration of driver in class scope and method scope). SeleneseTestngHelper is also selenium1.
Make sure you have the necessary webdriver jars in your project.
Certain commands in webdriver are different than selenium 1 and you might see compile erros when you do the replacement. Look at the methods available with driver. and use corresponding commands eg. Use get() of webdriver instead of open(). You can refer the javadocs for this or use your ide to get to know these.

Resources