It is a tool that can operate the browser automatically. It is often used in combination with tests because it can emulate user behavior.
First, drop the necessary libraries on Selenium's Download Page.
This time it's Java, so click on Java's ** Download **. (Click to start downloading the zip.)
Unzip it after downloading.
** client-combined-3.4.0-nodeps.jar ** is the main body of Selenium, and the ones under lib /
are the dependent libraries. (Everything below lib /
is also required to run Selenium.)
Since lib also contains JUnit
, you can use it immediately.
Also download the Chrome Driver from the Chrome Driver Download Page.
The latest version at the moment (2017/06/14) is 2.30, so I will drop it.
Please note that the supported Chrome version differs depending on the Chrome Driver version. (Chrome Driver 2.30 is Chrome v58-60)
Download the Chrome Driver that matches your OS.
Unzip the dropped zip and you're done downloading what you need!
Start Eclipse and create a new project. (The name is selenium-sample
.)
First, let's create a folder to put dependent files etc.
I created a folder (lib) for placing dependent libraries such as Selenium and a folder (exe) for placing ChromeDriver. Next, let's place the files in each folder.
Put everything under lib /
of selenium in the same folder.
Next, let's pass the build path.
Pass the build path to all the jars in lib. ** Be careful not to forget to press apply! ** **
Now that you're ready to run Selenium, let's get it working.
Create a new JavaClass in src. (The name is SampleTest.)
This time, I will use the sample code in Getting Started of Chrome Driver.
Change only the path to the Chrome Driver.
Java
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class SampleTest {
@Test
public void testGoogleSearch() throws InterruptedException {
// Optional, if not specified, WebDriver will search your path for chromedriver.
System.setProperty("webdriver.chrome.driver", "./exe/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com/xhtml");
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();
}
}
I think that this will work if you execute it. Please, try it.
If you try as above and it doesn't work, see the eclipse error message.
The driver is not executable. (Many Mac people) The cause is that ChromeDriver does not have execute permission, so please grant execute permission.
Execution file is not found (Many Windows people) Probably the path does not pass, so set the path appropriately.
If you clone this repository, you can use it immediately. (Note that the chrome driver is for Mac)
Recommended Posts