Java Selenium 패키지 -- RemoteWebDriver
package com.selenium.driver;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.RemoteWebElement;
public class JSWebDriver{
private RemoteWebDriver wd = null;
private JavascriptExecutor jse = null;
public JSWebDriver(URL remoteAddress, Capabilities desiredCapabilities) {
wd = new RemoteWebDriver(remoteAddress, desiredCapabilities);
}
///
/// url
///
public void goTo(String url){
wd.get(url);
}
///
///
///
public void quit(){
wd.quit();
}
///
///
///
public void back(){
wd.navigate().back();
}
///
///
///
public void forward(){
wd.navigate().forward();
}
///
///
///
public void refresh(){
wd.navigate().refresh();
}
///
/// ; title、url、index;
///
public void switchToWindow(String by, String value, String...match) throws Exception{
String currenthandle = wd.getWindowHandle();
Set<String> handles = wd.getWindowHandles();
int currentIndex = -1;
String searchString = "";
for(String handle : handles){
currentIndex += 1;
if(handle.equals(currenthandle)){
continue;
}else{
wd.switchTo().window(handle);
if (match.length == 1 && match[0].equals("regex")){
if (by.equals("title")){
searchString = wd.getTitle();
}else if (by.equals("url")){
searchString = wd.getCurrentUrl();
}
Pattern pattern = Pattern.compile(value);
Matcher matcher = pattern.matcher(searchString);
if(matcher.find()){
return;
}
}else{
if (by.equals("title")){
searchString = wd.getTitle();
}else if (by.equals("url")){
searchString = wd.getCurrentUrl();
}else if (by.equals("index")){
searchString = Integer.toString(currentIndex);
}
if(searchString.equals(value)){
return;
}
}
}
}
Exception e = new Exception("Swtich Window Failed, Please Make Sure The Locator Was Right.");
throw e;
}
///
///JS
///
public void clickAlertSure(){
Alert alert = wd.switchTo().alert();
alert.accept();
}
///
///JS
///
public void clickAlertDismiss()
{
Alert alert = wd.switchTo().alert();
alert.dismiss();
}
///
/// prompt
///
public void setPromptMessage(String parameter){
Alert alert = wd.switchTo().alert();
alert.sendKeys(parameter);
}
///
/// JS
///
public String getPromptMessage(){
Alert alert = wd.switchTo().alert();
return alert.getText();
}
///
/// Frame ; iframe
///
public void switchToFrame(JSWebElement jselement){
wd.switchTo().frame(jselement.getNativeWebElement());
}
///
/// JS
///
public void executeScript(String parameter){
JavascriptExecutor js = getJSE();
js.executeScript(parameter);
}
///
/// cookie
///
public String getCookie(String name){
Cookie cookie=wd.manage().getCookieNamed(name);
if (cookie == null){ return "null"; }
return cookie.getValue();
}
///
/// cookie
///
public Map<String, String> getCookies(){
Map<String, String> newCookies = new HashMap<String, String>();
Set<Cookie> cookies= wd.manage().getCookies();
for (Cookie cookie : cookies){
newCookies.put(cookie.getName(), cookie.getValue());
}
return newCookies;
}
///
///
///
public void getScreen(String filepath){
WebDriver augmentedDriver = new Augmenter().augment(this.wd);
TakesScreenshot ts = (TakesScreenshot) augmentedDriver;
File screenShotFile = ts.getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile (screenShotFile, new File(filepath));
}catch (IOException e){
e.printStackTrace();
}
}
///
/// title
///
public String getTitle(){
return wd.getTitle();
}
///
/// url
///
public String getUrl(){
return wd.getCurrentUrl();
}
///
/// HTML
///
public String getSource(){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
return wd.getPageSource();
}
///
///
///
public void scroll(String x, String y){
if (x.equals("left")){
x = "0";
}else if (x.equals("right")){
x = "document.body.scrollWidth";
}else if (x.equals("middle")){
x = "document.body.scrollWidth/2";
}
if (y.equals("top")){
y = "0";
}else if (y.equals("buttom")){
y = "document.body.scrollHeight";
}else if (y.equals("middle")){
y = "document.body.scrollHeight/2";
}
this.executeScript(String.format("scroll(%s,%s);", x, y));
}
///
///
///
public void maximize(){
wd.manage().window().maximize();
}
public JSWebElement findElementById(String using) {
try {
return new JSWebElement((RemoteWebElement)wd.findElementById(using));
}catch (NoSuchElementException e){
return new JSWebElement();
}
}
public JSWebElement findElementByCssSelector(String using) {
try {
return new JSWebElement((RemoteWebElement)wd.findElementByCssSelector(using));
}catch (NoSuchElementException e){
return new JSWebElement();
}
}
public JSWebElement findElementByXPath(String using) {
try {
return new JSWebElement((RemoteWebElement)wd.findElementByXPath(using));
}catch (NoSuchElementException e){
return new JSWebElement();
}
}
public JSWebElement findElementByLinkText(String using) {
try {
return new JSWebElement((RemoteWebElement)wd.findElementByLinkText(using));
}catch (NoSuchElementException e){
return new JSWebElement();
}
}
public JSWebElement findElementByDom(String using) {
try {
JavascriptExecutor js = this.getJSE();
WebElement we = (WebElement)js.executeScript(String.format("return %s", using));
return new JSWebElement((RemoteWebElement)we);
}catch (NoSuchElementException e){
return new JSWebElement();
}
}
///
/// RemoteWebdriver
///
public RemoteWebDriver getNativeWebDriver(){
return this.wd;
}
private JavascriptExecutor getJSE(){
if (this.jse == null){
this.jse = (JavascriptExecutor) this.wd;
}
return jse;
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.