Starting Selenium with custom Firefox profile from Eclipse - eclipse

I'm running Selenium tests from within Eclipse, but I can't load a custom Firefox profile.
Most sources suggest I need to launch the Selenium Server like this:
java -jar selenium-server.jar -firefoxProfileTemplate </path/to/template/>
But when launching my test from within Eclipse it doesn't use that - the tests will run if the Selenium Server isn't running.
This thread suggests that I can set the profile in the DefaultSelenium constructor:
Selenium RC - disabling browser cookie
But the code generated for me by Selenium IDE (Firefox plugin) doesn't use that constructor:
package com.example.tests;
import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;
public class Example extends SeleneseTestCase {
public void setUp() throws Exception {
setUp("http://www.example.com/", "*firefox");
}
public void testExample() throws Exception {
selenium.open("/");
selenium.click("//body");
}
}
Where should I set the DefaultSelenium configuration options? Or is there some other method I can use to load my custom Firefox template?
Thanks!
Stu

I made a SeleniumTestCase that starts/stops the server before/after each test class and starts/stops the Selenium instance before/after each test:
public class SeleniumTestCase {
protected static Selenium selenium;
protected static AppNavUtils appNavUtils;
#BeforeClass
public static void setUpBeforeClass() throws Exception {
SeleniumServerControl.getInstance().startSeleniumServer();
}
#AfterClass
public static void tearDownAfterClass() throws Exception {
SeleniumServerControl.getInstance().stopSeleniumServer();
}
#Before
public void setUp() throws Exception {
// Replace "*chrome" with "*firefox" for Selenium > 1.0
selenium = new DefaultSelenium("localhost", 5444, "*chrome", "http://localhost:8080/");
selenium.start();
appNavUtils = new AppNavUtils(selenium);
}
#After
public void tearDown() throws Exception {
selenium.stop();
}
}
The SeleniumServerControl starts and stops the server:
public class SeleniumServerControl {
private static final SeleniumServerControl instance = new SeleniumServerControl();
public static SeleniumServerControl getInstance()
{
return instance;
}
private SeleniumServer server = null;
protected SeleniumServerControl(){}
public void startSeleniumServer() {
if (server == null) {
RemoteControlConfiguration rcc = new RemoteControlConfiguration();
rcc.setPort(5444);
//rcc.setFirefoxProfileTemplate(newFirefoxProfileTemplate)
server = new SeleniumServer(rcc);
}
server.start();
}
public void stopSeleniumServer()
{
if (server != null) {
server.stop();
server = null;
}
}
}

the version of code you have above assumes that you are running your tests against localhost on port 4444 thats why it is has 2 parameters in the setup.
To set up eclipse to run it you will need to update the run configuration. That is under
Run > Run Configurations
Have a look for the item that has selenium in it and add the config above so that when it runs it will pick it up and run.
I personally just fire up the server when I start working by running a batch file and kill it at the end of the day.

Related

Javax.naming.NameNotFoundException: when trying to look up the jndi name in JavaEE and Wildfly

I am doing some unit testing using javaee and wildly so I am in the client project trying to lookup the jndi name remotely in order to test a service located of course in the ejb, all is fine and the handshake is done correctly but while testing I am getting an exeception which is:
test.ServiceLocatorException: javax.naming.NameNotFoundException:
/phenomenon-ejb/ClientSessionBean!utilities.CllientSessionBeanRemote
Here is a snippet showing the appname, modulename and this is how I am trying to lookup the jndi:
java:global/phenomenon-ear/phenomenon-ejb/ClientSessionBean!utilities.CllientSessionBeanRemote
java:app/phenomenon-ejb/ClientSessionBean!utilities.CllientSessionBeanRemote
java:module/ClientSessionBean!utilities.CllientSessionBeanRemote
java:global/phenomenon-ear/phenomenon-ejb/ClientSessionBean!utilities.CllientSessionBeanLoacal
java:app/phenomenon-ejb/ClientSessionBean!utilities.CllientSessionBeanLoacal
java:module/ClientSessionBean!utilities.CllientSessionBeanLoacal
Client Code:
public class JunitTester {
public static final String jndi = "java:global/phenomenon-ear/phenomenon-ejb/ClientSessionBean!utilities.CllientSessionBeanRemote" ;
private static CllientSessionBeanRemote getProxy() {
return (CllientSessionBeanRemote) ServiceLocator.getInstance().getProxy(jndi);
}
#org.junit.Test
public void Test() throws NamingException {
System.out.println(JunitTester.getProxy().Verify_No_Existence("Multiskan"));}
Any advice could help, thanks.
with your client code I was able to run on glassfish successfully....
Below is the code having said that
if you can test again please give me the exact exception.
Ensure Client.jar is in your classpath in my case its gf-client.jar
server up and running and you than connect the client
Junit
package com.au.clients;
import com.au.ejbs.GreetingI;
import javax.naming.NamingException;
public class JunitTester {
public static final String jndi = "java:global/ejb3_2_ear_exploded/ejb/Greeting";
private static GreetingI getProxy() throws NamingException {
return (GreetingI) ServiceLocator.getInstance().getProxy(jndi);
}
#org.junit.Test
public void Test() throws NamingException {
System.out.println(getProxy().Verify_No_Existence("Multiskan"));
}
}
Service locator
import com.au.ejbs.GreetingI;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class ServiceLocator {
public static ServiceLocator getInstance() {
ServiceLocator serviceLocator = null;
if(serviceLocator == null) {
serviceLocator = new ServiceLocator();
}
return serviceLocator;
}
public static GreetingI getProxy(String jndi) throws NamingException {
InitialContext context =new InitialContext();
GreetingI greeting = context.doLookup(jndi);
return greeting;
}
}
output
true
Process finished with exit code 0

Create single WireMockServer object with single port

I am very new to Wire mock and gradle. I am planning to setup by using single WireMockServer object with using 8081 port and its configurable in gradle task.
build.gradle
plugins {
id 'org.springframework.boot' version '2.2.6.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'com.williamhill.wiremock' version '0.4.1'
id 'java'
}
wiremock {
dir "src/test/resources/"
params "--port=8081"
}
task integrationTest(type: Test) {
runWithWiremock = true
}
test class :
#RunWith(SpringRunner.class)
#SpringBootTest(classes = GladletaskApplication.class)
#AutoConfigureWireMock
#ActiveProfiles(value = "integration")
public class StudentControllerTest {
#Rule
public WireMockRule wireMockRule = new WireMockRule();
#Autowired
public TestRestTemplate testRestTemplate;
#Before
public void setUp() {
mockRemoteService();
}
#Test
public void testAllStudents() {
ResponseEntity<List<Student>> responseEntity = testRestTemplate.exchange("http://localhost:8093/all", HttpMethod.GET,null,
new ParameterizedTypeReference<List<Student>>(){}
);
List<Student> studentList = responseEntity.getBody();
System.out.println("StudentList--->" + studentList.size());
}
private void mockRemoteService() {
stubFor(get(urlEqualTo("/all-students"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBodyFile("response.json")
));
}
}
Now target is i have 8081 server up only once and run all the test cases and shutdown last
is it possible to do it?
could please provide springboot-wiremock gradle custom task?
I have solved by using the following :
1) #BeforeClass & #AfterClass start and stop the server.
2) I tried with a created JUnit test suite and #BeforeClass & #AfterClass start and stop the server, here going apply suite level.

Unable to open chromedriver in eclipse

I'm facing errors while trying to execute a simple code to launch a chrome browser and open google.com but not able to launch the browser. Attached is the error message.
This is the code -
package gmailpkg;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
public class login {
public static String driverPath = "X:/JARs-Setup/chromedriver_win32/";
public static WebDriver driver;
public static void main(String []args) throws InterruptedException {
System.out.println("launching chrome browser");
System.setProperty("webdriver.chrome.driver", driverPath+"chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://google.com");
Thread.sleep(2000);
//driver.manage().window().maximize();
Thread.sleep(2000);
driver.findElement(By.xpath( "//*[#href='https://mail.google.com/mail/?tab=wm']")).click();
}
}

open javafx application via rest call

Does anyone know how to open a JavaFx application via rest requests?
Scenario:
I have a Spring Boot Service that runs on every machine to make a bridge wiht my SPA(Single Page Application) and my comm ports. Thats because SPA cannot talk to the OS.
This communications are made via http requests.
Now I have a problem, I need to make a javafx application that is started via http request, when I call the firts time, it works ok, but if i close the javafx application clicking on 'x' and try to open again I'm getting the following error:
java.lang.IllegalStateException: Application launch must not be called more than once
There is a way that when I close the javafx window, it kill the JavaFx Thread, so when I call it again, it start a new Thread?
Or do I need to keep the Thread and just find a way to reopen my javafx application in the same Thread?
here is my #Controller
#RestController
#RequestMapping("v1/appfx")
public class AppfxController{
#RequestMapping("")
private void openJavaFxApp(){
try{
MyJavaFxApp.launch(MyJavaFxApp.class);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
And Here is my JavaFx:
public class MyJavaFxApp extends Application {
private final static Logger log = LoggerFactory.getLogger(MyJavaFxApp.class);
private BigDecimal left;
private String selectedOperator;
private boolean numberInputting;
#FXML
private TextField display;
public MyJavaFxApp() {
this.left = BigDecimal.ZERO;
this.selectedOperator = "";
this.numberInputting = false;
}
#Override
public void start(Stage stage) throws Exception {
stage.setTitle("MyJavaFxApp");
stage.setOnCloseRequest(x -> {
log.info("closed");
Platform.exit();
});
stage.setResizable(false);
stage.setScene(new Scene(FXMLLoader.load(getClass().getClassLoader().getResource("Test.fxml"))));
stage.show();
stage.toFront();
}
}
Thanks for any help.

NullPointerException when I changed all my methods to static

Using Selenium w Java and Test NG (POM format) Switched everything from not static to static and changed everything accordingly, getting NullPointerException. It worked when everything was not static, but making everything static requires less code so I'd prefer to have it that way.
Here is my code.. while trying to paste my code for some reason it did not recognize the import statements as code so I just did not include them, but rest assured everything has been imported that is needed! :)
Package pages;
public class locationPage {
WebDriver driver;
static #FindBy (id="btn_bogota") WebElement chooseBogota;
static #FindBy (id="btn_medellin") WebElement chooseMedellin;
static #FindBy (xpath="//title") WebElement pageTitle;
public locationPage (WebDriver driver){
this.driver=driver;
PageFactory.initElements(driver, this);
}
public static void chooseLocation (String location) {
if (location.equals("Bogota"))
{
chooseBogota.click();
}
else if (location.equals("Medellin")){
chooseMedellin.click();
}
}
}
Package testcases;
public class selectLocation {
WebDriver driver;
#BeforeClass
public void setup() throws InterruptedException{
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://dev01.juanregala.com.co/");
//Generating Alert Using Javascript Executor
JavascriptExecutor javascript = (JavascriptExecutor) driver;
javascript.executeScript("alert('Select Location');");
Thread.sleep(2000);
driver.switchTo().alert().accept();
}
#AfterTest
public void quit(){
driver.quit();
}
#Test (priority=0)
public void location(){
locationPage.chooseLocation("Medellin");
}
}
It keeps giving me a NullPointerException referring to
chooseMedellin.click();
and
locationPage.chooseLocation("Medellin");
I am not really sure how else to describe my issue :( Please help!
You are getting a NullPointerException because the fields are not initialized. You can't use a page object in this way. Even if you could, you probably wouldn't want to since a page object is tied to a particular web driver and that would mean you could never use the same page object among multiple tests and run them in parallel.
Basically, the PageFactory only works on object instances. It won't initialize static fields and, even if it did, you are only calling the page factory from the constructor of your page and that is never being called since you only call a static method on that class.
You need to create an instance of the page object and then pass it to the PageFactory along with the driver that will be running the page object.
Simple example:
public class FooTest {
private WebDriver driver;
private FooPage page;
#BeforeMethod
public void setup() {
driver = new FirefoxDriver();
page = PageFactory.initElements(driver, FooPage.class);
}
#AfterMethod
public void tearDown() {
try {
driver.quit();
} catch ( Exception ignore ) { }
driver = null;
page = null;
}
#Test
public void testFoo() {
}
}
public class FooPage {
#FindBy(id="foo")
private WebElement fooElement;
public void clickFoo() {
fooElement.click();
}
}
Note the line:
page = PageFactory.initElements(driver, FooPage.class);
That method will actually call the default construct of FooPage. You can also instantiate the object yourself and pass it to the page factory like this:
page = new FooPage();
PageFactory.initElements(driver, page);