Skip to content

Instantly share code, notes, and snippets.

@hidepin
Created March 17, 2016 21:29
Show Gist options
  • Save hidepin/476e17bab3930cd6a3ad to your computer and use it in GitHub Desktop.
Save hidepin/476e17bab3930cd6a3ad to your computer and use it in GitHub Desktop.
selenium_useragent
package org.func.sample.selnium;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
public class Selenium {
public static void main(String[] args) {
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
FirefoxProfile profile = new FirefoxProfile();
RemoteWebDriver remoteWD = null;
String json = "";
int sleeptime = 2000;
try {
profile.setPreference("general.useragent.override", "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7");
capabilities.setCapability(FirefoxDriver.PROFILE, profile);
remoteWD = new RemoteWebDriver(new URL("http://192.168.137.225:4444/wd/hub"), capabilities);
// remoteWD.get("file://C:/selenium/Chapter 3/HTML/Frames.html");
remoteWD.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
remoteWD.navigate().to("https://www.google.co.jp");
WebElement searchBox = remoteWD.findElement(By.name("q"));
searchBox.sendKeys("Selenium WebDriver");
WebElement searchButton = remoteWD.findElement(By.name("btnG"));
searchButton.click();
Thread.sleep(sleeptime);
searchBox = remoteWD.findElement(By.name("q"));
searchBox.clear();
Thread.sleep(sleeptime);
searchBox.sendKeys("Packt Publishing");
searchButton = remoteWD.findElement(By.name("btnG"));
searchButton.click();
Thread.sleep(sleeptime);
remoteWD.navigate().back();
Thread.sleep(sleeptime);
remoteWD.navigate().forward();
Thread.sleep(sleeptime);
remoteWD.navigate().refresh();
WebDriver augmentedDriver = new Augmenter().augment(remoteWD);
File screenshot = ((TakesScreenshot) augmentedDriver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshot, new File("C:/work/test.png"));
Thread.sleep(sleeptime);
remoteWD.quit();
} catch (InterruptedException | IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment