Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.gamebuster19901.GameBuSK.effects;
- import java.io.File;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Map.Entry;
- import org.bukkit.event.Event;
- import org.openqa.selenium.By;
- import org.openqa.selenium.Keys;
- import org.openqa.selenium.NoSuchElementException;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.WebDriverException;
- import org.openqa.selenium.WebElement;
- import org.openqa.selenium.chrome.ChromeDriver;
- import org.openqa.selenium.firefox.FirefoxBinary;
- import org.openqa.selenium.firefox.FirefoxDriver;
- import org.openqa.selenium.firefox.FirefoxProfile;
- import com.gamebuster19901.GameBuSK.GameBuSK;
- import com.gamebuster19901.GameBuSK.exceptions.AlreadyExistsException;
- import com.gamebuster19901.GameBuSK.misc.Debug;
- import com.gamebuster19901.GameBuSK.types.BrowserType;
- import com.gamebuster19901.GameBuSK.types.SearchType;
- import ch.njol.skript.classes.ClassInfo;
- import ch.njol.skript.classes.Parser;
- import ch.njol.skript.lang.Expression;
- import ch.njol.skript.lang.ParseContext;
- import ch.njol.skript.lang.SkriptParser.ParseResult;
- import ch.njol.skript.lang.util.SimpleExpression;
- import ch.njol.skript.registrations.Classes;
- import ch.njol.util.Kleenean;
- public class GWebDriver extends SimpleExpression<GWebDriver>{
- public WebDriver driver;
- public String name;
- public Object type;
- public static Map<String, GWebDriver> GWebDrivers = new HashMap<String, GWebDriver>();
- public static void register() {
- Classes.registerClass(new ClassInfo<GWebDriver>(GWebDriver.class, "browser").user("browser").name("browser")
- .parser(new Parser<GWebDriver>() {
- @Override
- public GWebDriver parse(String s, ParseContext context) {
- return null;
- }
- @Override
- public String getVariableNamePattern() {
- // TODO Auto-generated method stub
- return ".+";
- }
- @Override
- public String toString(GWebDriver locDriver, int flags) {
- // TODO Auto-generated method stub
- return getKey(locDriver);
- }
- @Override
- public String toVariableNameString(GWebDriver locDriver) {
- // TODO Auto-generated method stub
- return getKey(locDriver);
- }
- }
- ));
- }
- public static String getKey(GWebDriver locDriver){
- for(Entry<String, GWebDriver> entry : GWebDrivers.entrySet()){
- if (locDriver.equals(entry.getValue())){
- return entry.getKey();
- }
- }
- return locDriver.toString();
- }
- public static GWebDriver getWindow(String s){
- GWebDriver locDriver = GWebDrivers.get(s);
- return locDriver;
- }
- public static String[] getAllKeys(){
- String[] strings = GWebDriver.GWebDrivers.keySet().toArray(new String[GWebDriver.GWebDrivers.size()]);
- return strings;
- }
- public static void create(BrowserType finalType, String name) throws AlreadyExistsException {
- GWebDriver value = GWebDrivers.get(name);
- if (value == null) {
- GWebDriver createdDriver = new GWebDriver();
- if (finalType == BrowserType.FIREFOX) {
- try{
- FirefoxBinary binary = new FirefoxBinary(GameBuSK.firefoxFL);
- FirefoxProfile profile = new FirefoxProfile();
- WebDriver theDriver = new FirefoxDriver(binary,profile);
- System.out.println("GWebDriver createdDriver is " + createdDriver);
- createdDriver.driver = theDriver;
- System.out.println("Driver: " + createdDriver.driver);
- createdDriver.name = name;
- System.out.println("Name: " + createdDriver.name);
- createdDriver.type = BrowserType.FIREFOX;
- System.out.println("Type: " + createdDriver.type);
- GWebDrivers.put(name, createdDriver);
- }
- catch (WebDriverException e){
- if (GameBuSK.debug == true){
- e.printStackTrace();
- }
- System.out.println("[SEVERE]: You have not enabled firefox in the config. Stop the server and enable firefox in the config, then restart the server");
- }
- }
- if (finalType == BrowserType.CHROME) {
- System.out.println(BrowserType.CHROME);
- try{
- System.out.println(1);
- WebDriver theDriver = new ChromeDriver();
- System.out.println("GWebDriver createdDriver is " + createdDriver);
- createdDriver.driver = theDriver;
- System.out.println("Driver: " + createdDriver.driver);
- createdDriver.name = name;
- System.out.println("Name: " + createdDriver.name);
- createdDriver.type = BrowserType.CHROME;
- System.out.println("Type: " + createdDriver.type);
- GWebDrivers.put(name, createdDriver);
- System.out.println(GWebDriver.GWebDrivers + "test");
- }
- catch (IllegalStateException e){
- try{
- if (GameBuSK.debug == true){
- e.printStackTrace();
- }
- System.out.println("[SEVERE]: You have not enabled chrome in the config. Stop the server and enable chrome in the config, then restart the server");
- }
- catch (NullPointerException e2){
- System.out.println("non-bukkit environment");
- }
- }
- }
- if (finalType == BrowserType.GHOST){
- //TODO add ghost driver
- }
- }
- else{
- throw new AlreadyExistsException(name);
- }
- }
- /*
- *
- *
- *
- *
- *
- * Instance stuff
- *
- *
- *
- *
- *
- *
- */
- public void stop(){
- String key = getKey(this);
- GWebDrivers.remove(key);
- driver.quit();
- }
- public WebDriver type(){
- return (WebDriver) type;
- }
- public WebDriver getDriver(){
- return driver;
- }
- public String getName(){
- return name;
- }
- public BrowserType getType(){
- if (driver instanceof FirefoxDriver) {
- return BrowserType.FIREFOX;
- }
- if (driver instanceof ChromeDriver){
- return BrowserType.CHROME;
- }
- else{
- return null;
- }
- }
- public WebElement GetElement(SearchType searchType, String parameter){
- WebElement element;
- try{
- if (searchType == SearchType.CLASS_NAME){
- element = driver.findElement(By.className(parameter));
- return element;
- }
- else if (searchType == SearchType.CSS_SELECTOR){
- element = driver.findElement(By.cssSelector(parameter));
- return element;
- }
- else if (searchType == SearchType.ID){
- element = driver.findElement(By.id(parameter));
- return element;
- }
- else if (searchType == SearchType.LINK_TEXT){
- element = driver.findElement(By.linkText(parameter));
- return element;
- }
- else if (searchType == SearchType.NAME){
- System.out.println("Driver is: " + driver);
- System.out.println("Type is: " + type);
- System.out.println("Name is: " + name);
- System.out.println("Searching by: " + searchType);
- element = driver.findElement(By.name(parameter));
- if (element instanceof WebElement){
- System.out.println(element + " is a Web element");
- }
- System.out.println(element);
- System.out.println(element.getClass());
- return element;
- }
- else if (searchType == SearchType.PARTIAL_LINK_TEXT){
- element = driver.findElement(By.partialLinkText(parameter));
- return element;
- }
- else if (searchType == SearchType.TAG_NAME){
- element = driver.findElement(By.tagName(parameter));
- return element;
- }
- else if (searchType == SearchType.XPATH){
- element = driver.findElement(By.xpath(parameter));
- return element;
- }
- }
- catch (NoSuchElementException e){
- if (GameBuSK.debug == true){
- e.printStackTrace();
- }
- System.out.println("No element was found using " + type + " with parameter \"" + parameter + "\" in " + driver.getTitle());
- return null;
- }
- return null;
- }
- public void newTab() {
- try {
- if (GameBuSK.os.contains("Mac") || GameBuSK.os.contains("OS X")) {
- Debug.Debug("newtab MAC");
- driver.findElement(By.cssSelector("body")).sendKeys(Keys.COMMAND + "t");
- } else {
- Debug.Debug("newtab other");
- Debug.Debug("driver");
- Debug.Debug("------------------------------------------------");
- Debug.Debug("Driver: " + driver);
- Debug.Debug("Name: " + name);
- Debug.Debug("Type: " + type);
- Debug.Debug("Driver: " + driver);
- Debug.Debug("This: " + this);
- driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");
- }
- } catch (NullPointerException e) { //This will be null in a non-bukkit environment, in which case it assumes it's running on windows
- System.out.println("Possible non-bukkit environment detected, assuming operating system is WINDOWS and trying again. A Nullpointer exception will appear below.");
- e.printStackTrace();
- driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");
- }
- }
- public void goTo(String URL){
- try{
- driver.get(URL);
- }
- catch (WebDriverException e){
- System.out.println(URL + " is not a valid url, cannot go there");
- }
- }
- /*
- *
- *
- *
- *
- *
- */
- private Expression<String> objname;
- @Override
- public Class<? extends GWebDriver> getReturnType() {
- // TODO Auto-generated method stub
- return GWebDriver.class;
- }
- @Override
- public boolean isSingle() {
- // TODO Auto-generated method stub
- return true;
- }
- @Override
- public boolean init(Expression<?>[] expr, int matchedPattern, Kleenean arg2, ParseResult arg3) {
- // TODO Auto-generated method stub
- objname = (Expression<String>) expr[0];
- return true;
- }
- @Override
- public String toString(Event arg0, boolean arg1) {
- // TODO Auto-generated method stub
- return "browser";
- }
- @Override
- protected GWebDriver[] get(Event e) {
- // TODO Auto-generated method stub
- return new GWebDriver[]{this};
- }
- /*
- public static GWebDriver getWindow(String s){
- GWebDriver locDriver = GWebDrivers.get(s);
- return locDriver;
- }
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement