Launch Firefox using Selenium - maven

I am Trying to lunch Firefox and do a simple search on Google but i am only able to launch the Firefox. what should be edit in the code to run it smooth?I am Using Firefox Quantum 57 which runs using maven dependencies.
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FirefoxLaunch
{
#Test
public static void main(String [] args) {
WebDriver driver;
driver = new FirefoxDriver();
driver.get("https://google.com");
WebElement element =driver.findElement(By.name("q"));
element.sendKeys("FirefoxDriver Search Function");
driver.quit();
}
}

While working with:
Selenium v3.x java clients
Firefox Quantum v70.x
You need to download the geckodriver binary from this link and save it in your system and then provide the absolute path of GeckoDriver binary through System.setProperty() line as follows :
System.setProperty("webdriver.gecko.driver", "C:\\your_directory\\geckodriver.exe");

Related

load chromedriver in spring boot

I'm running a spring boot application and I'm trying to get the chromedriver not load from my local directory but rather from the project resources folder. I have my chromdriver.exe in resources/chromedriver.exe but I'm not sure how can I load it.
I tried this didn't work. Tried filepath to be "resources/chromedriver.exe" didn't work as well
String filePath = ClassLoader.getSystemClassLoader().getResource("resources/chromedriver.exe").getFile();
System.out.println(filePath);
System.setProperty("webdriver.chrome.driver", filePath)
If you are using Spring, you could try this:
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
Resource resource = new ClassPathResource("chromedriver.exe");
String filePath = resource.getFile().getPath();
System.out.println(filePath);
System.setProperty("webdriver.chrome.driver", filePath);
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.*;
import org.junit.Test;
public class GettingStarted {
#Test
public void testGoogleSearch() throws InterruptedException {
// Optional. If not specified, WebDriver searches the PATH for chromedriver.
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com/");
Thread.sleep(5000); // Let the user actually see something!
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys("ChromeDriver");
searchBox.submit();
Thread.sleep(5000); // Let the user actually see something!
driver.quit();
}
}
https://sites.google.com/a/chromium.org/chromedriver/getting-started
For example in Windows if chromedriver.exe is in C:/chromium use:
System.setProperty("webdriver.chrome.driver", "C:\\chromium\\chromedriver.exe");

Pig store fails to run store command when it is called using java code (embedded mode)

I am learning Hadoop, I tried running my pig script using java but it seems like it skips the store command written in the script and does not produce the output data file at the particular location.
But when I try running the pig script using command line, it gives the output data file as desired.
First I thought that java might have some permission issues that it is not creating a file. But I tried creating a file in the exact location using java and it easily creates an empty file. So it doesn't seem to be a permission issue.
Can anybody tell me what is the issue here that pig script runs successfully when used through command line but fails in the embedded mode?
Java Code:
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import org.apache.pig.PigServer;
import java.io.IOException;
public class storePig {
public static void main(String args[]) throws Exception {
try {
PigServer pigServer = new PigServer("local");
runQuery(pigServer);
}catch (Exception e){
e.printStackTrace();
}
}
public static void runQuery(PigServer pigServer) throws IOException {
pigServer.registerScript("/home/anusharma/Desktop/stackoverflow/sampleScript.pig");
}
}
Pig-Script:
Employee = LOAD '/home/anusharma/Desktop/Hadoop/Pig/record.txt' using PigStorage(',') as (id:int, firstName:chararray, lastName:chararray, age:int, contact:chararray, city:chararray);
Employe = ORDER Employee BY age desc;
limitedEmployee = LIMIT Employe 4;
STORE limitedEmployee into '/home/anusharma/Desktop/stackoverflow/output' using
PigStorage('|');

How include image in .jar file for exporting into .exe

i made this code:
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class ExplaneImage {
public static void main( String[] args )
{
Image image = null;
try {
File sourceimage = new File
("C:\\...\\ExplaneImage\\im1.jpg");
image = ImageIO.read(sourceimage);
} catch (IOException e) {}
JFrame frame = new JFrame();
frame.setSize(496, 325);
JLabel label = new JLabel(new ImageIcon(image));
frame.add(label);
frame.setVisible(true);
}
}
I'll need export this first in .jar file, after in .exe file (I have found the program to convert it), but it doesn't work cause exportation was made from class and not from a package, and the picture is not included in class.
I would like to make a simple images viewer for export in .exe format (to use on computers that do not have the jdk and jre) , independent, also containing images.
Thanks a lot for your help

Swing apllication with embedded JavaFX WebView won't play html5 video only sound

In my Swing application I needed support for rendering html. So I embedded a JavaFX WebView in my Swing application. Now on some html pages I use the new html5 -Tag to play a video. This works perfectly on Windows and Linux. But on MacOS I only hear the sound and see a black video frame and the time track in the bottom.
Here is an SSCCE I got from github. I just changed the url to one that contains a html5 video-tag example. Would be great, if you MacOS users could try it and tell me if the same happens on you computer. And of course any idea to fix this is appreciated.
SSCCE:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javafx.application.Platform;
import javafx.collections.ObservableList;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import com.sun.javafx.application.PlatformImpl;
/**
* SwingFXWebView
*/
public class JavaFXTest extends JPanel
{
private Stage stage;
private WebView browser;
private JFXPanel jfxPanel;
private JButton swingButton;
private WebEngine webEngine;
private Object geo;
public JavaFXTest()
{
this.initComponents();
}
public static void main(final String... args)
{
// Run this later:
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
final JFrame frame = new JFrame();
frame.getContentPane().add(new JavaFXTest());
frame.setMinimumSize(new Dimension(640, 480));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
private void initComponents()
{
this.jfxPanel = new JFXPanel();
this.createScene();
this.setLayout(new BorderLayout());
this.add(this.jfxPanel, BorderLayout.CENTER);
this.swingButton = new JButton();
this.swingButton.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(final ActionEvent e)
{
Platform.runLater(new Runnable()
{
#Override
public void run()
{
JavaFXTest.this.webEngine.reload();
}
});
}
});
this.swingButton.setText("Reload");
this.add(this.swingButton, BorderLayout.SOUTH);
}
/**
* createScene Note: Key is that Scene needs to be created and run on
* "FX user thread" NOT on the AWT-EventQueue Thread
*/
private void createScene()
{
PlatformImpl.startup(new Runnable()
{
#Override
public void run()
{
JavaFXTest.this.stage = new Stage();
JavaFXTest.this.stage.setTitle("Hello Java FX");
JavaFXTest.this.stage.setResizable(true);
final Group root = new Group();
final Scene scene = new Scene(root, 80, 20);
JavaFXTest.this.stage.setScene(scene);
// Set up the embedded browser:
JavaFXTest.this.browser = new WebView();
JavaFXTest.this.webEngine = JavaFXTest.this.browser.getEngine();
JavaFXTest.this.webEngine.load("http://camendesign.com/code/video_for_everybody/test.html");
final ObservableList<Node> children = root.getChildren();
children.add(JavaFXTest.this.browser);
JavaFXTest.this.jfxPanel.setScene(scene);
}
});
}
}
Here is a semi-answer, which might help:
The oracle website states:"At this time, Online Installation and Java Update features are not available for 64-bit architectures"
For me this caused lots of problems, because Java seems up to date, but actually isn't. On some machines I could solve the actual issue by just manually updating the Java 64bit VM. On Mac however, the video still isn't playing, only sound.
The 64bit/32bit issue gets even worse, since a double click on a jar might start it in the 64bit JVM, but via console it is started in 32bit JVM. So if you do a "java -version" in console, the output might be "1.7.0 u45 32-bit", but as soon as you start the jar via double click it is started in an outdated 64bit JVM.
So if you ever run in an JavaFX issue (especially with UnsatisfiedLinkError) and you have a 64bit computer, just install the latest 64bit java and hope that it solves the problem.

ERROR: Element Not Found works well in Selenium IDE but not in Selenium RC. getting error

this is my script. while clicking a link using its xpath not working & throws an error 'ERROR element not found' but works well in Selenium IDE.
import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.regex.Pattern;
public class testGoogle extends SeleneseTestCase {
#Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.google.co.in/");
selenium.start();
selenium.windowFocus();
selenium.windowMaximize();
}
#Test
public void testUntitled() throws Exception {
selenium.open("http://www.google.co.in/");
selenium.waitForPageToLoad("30000");
assertEquals("Google", selenium.getTitle());
selenium.click("name=q");
selenium.type("name=q", "Software Testing");
selenium.click("name=btnK");
selenium.click("//html/body/div[4]/div/div/div[4]/div[3]/div[2]/div/div[2]/div/ol/li[2]/div/h3/a"); // ERROR: Element Not Found
selenium.waitForPageToLoad("30000");
}
#After
public void tearDown() throws Exception {
selenium.stop();
}
}
You can detect link as selenium.click("link=xyz") instead of xpath
The script is absolutely right. But the problem is that it is running very fast. When you start the script all the elements are not fully loaded. So add
selenium.setSpeed("1000");
as the very first line in function testUntitled(). Your script will run fine.
Cheers,
Amit Shakya
Just add "xpath=" before the path like this
xpath=/html/body/div[4]/div/div/div[4]/div[3]/div[2]/div/div[2]/div/ol/li[2]/div/h3/a
it will work faced this problem earlier..
I was also facing the issue - "Element not found" in Selenium RC.
Using selenium.setSpeed("1000") worked.

Resources