Java 在 Katalon-Studio 中编写脚本
Java scripting in Katalon-Studio
我编写了几个 java 测试并用于 运行 Eclipse。
我想将它们导入katalon并运行它们。
比如我这里有一个登录脚本:
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.*;
public class Login {
public static void main(String args[]) throws IOException {
IOException ioe = new IOException();
//Initializing server
System.setProperty("webdriver.chrome.driver", "C:/selenium/chromedriver.exe");
ChromeDriver wd = new ChromeDriver();
wd.manage().window().maximize();
wd.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//login
System.out.println("*** login ***");
wd.get("<URL>");
wd.findElement(By.xpath("//form[@id='form']/div[1]/paper-input/paper-input-container/div[2]/div/input")).click();
wd.findElement(By.xpath("//form[@id='form']/div[1]/paper-input/paper-input-container/div[2]/div/input")).clear();
wd.findElement(By.xpath("//form[@id='form']/div[1]/paper-input/paper-input-container/div[2]/div/input")).sendKeys("<USERNAME>");
wd.findElement(By.xpath("//form[@id='form']/div[2]/paper-input/paper-input-container/div[2]/div/input")).click();
wd.findElement(By.xpath("//form[@id='form']/div[2]/paper-input/paper-input-container/div[2]/div/input")).clear();
wd.findElement(By.xpath("//form[@id='form']/div[2]/paper-input/paper-input-container/div[2]/div/input")).sendKeys("<PASSWORD>");
wd.findElement(By.xpath("//form[@id='form']//paper-button[.='login']")).click();
try { Thread.sleep(3000l); } catch (Exception e) { throw new RuntimeException(e); }
if(wd.findElement(By.tagName("html")).getText().contains("please login")){
System.out.println("Login failed");
throw ioe;
}//End of login
System.out.println("Login was executed successfully!");
System.out.println("Testcase finished successfully!");
wd.quit();
}
}
我想 运行 它就像在 katalon 中一样,但我不确定如何。
谢谢。
我尝试添加现有的 java 脚本而不声明 class 和 main 方法,它起作用了。
在您的示例中,请删除: import org.openqa.selenium.*; ,将其替换为: import org.openqa.selenium.By 然后粘贴剩余脚本 without
public class Login {
public static void main(String args[]) throws IOException {
}}
因此您在 Katalon 中的自定义测试用例将是:
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.By;
IOException ioe = new IOException();
//Initializing server
System.setProperty("webdriver.chrome.driver", "C:/selenium/chromedriver.exe");
ChromeDriver wd = new ChromeDriver();
wd.manage().window().maximize();
wd.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//login
System.out.println("*** login ***");
wd.get("<URL>");
wd.findElement(By.xpath("//form[@id='form']/div[1]/paper-input/paper-input-container/div[2]/div/input")).click();
wd.findElement(By.xpath("//form[@id='form']/div[1]/paper-input/paper-input-container/div[2]/div/input")).clear();
wd.findElement(By.xpath("//form[@id='form']/div[1]/paper-input/paper-input-container/div[2]/div/input")).sendKeys("<USERNAME>");
wd.findElement(By.xpath("//form[@id='form']/div[2]/paper-input/paper-input-container/div[2]/div/input")).click();
wd.findElement(By.xpath("//form[@id='form']/div[2]/paper-input/paper-input-container/div[2]/div/input")).clear();
wd.findElement(By.xpath("//form[@id='form']/div[2]/paper-input/paper-input-container/div[2]/div/input")).sendKeys("<PASSWORD>");
wd.findElement(By.xpath("//form[@id='form']//paper-button[.='login']")).click();
try { Thread.sleep(3000l); } catch (Exception e) { throw new RuntimeException(e); }
if(wd.findElement(By.tagName("html")).getText().contains("please login")){
System.out.println("Login failed");
throw ioe;
}//End of login
System.out.println("Login was executed successfully!");
System.out.println("Testcase finished successfully!");
wd.quit();
我编写了几个 java 测试并用于 运行 Eclipse。
我想将它们导入katalon并运行它们。
比如我这里有一个登录脚本:
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.*;
public class Login {
public static void main(String args[]) throws IOException {
IOException ioe = new IOException();
//Initializing server
System.setProperty("webdriver.chrome.driver", "C:/selenium/chromedriver.exe");
ChromeDriver wd = new ChromeDriver();
wd.manage().window().maximize();
wd.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//login
System.out.println("*** login ***");
wd.get("<URL>");
wd.findElement(By.xpath("//form[@id='form']/div[1]/paper-input/paper-input-container/div[2]/div/input")).click();
wd.findElement(By.xpath("//form[@id='form']/div[1]/paper-input/paper-input-container/div[2]/div/input")).clear();
wd.findElement(By.xpath("//form[@id='form']/div[1]/paper-input/paper-input-container/div[2]/div/input")).sendKeys("<USERNAME>");
wd.findElement(By.xpath("//form[@id='form']/div[2]/paper-input/paper-input-container/div[2]/div/input")).click();
wd.findElement(By.xpath("//form[@id='form']/div[2]/paper-input/paper-input-container/div[2]/div/input")).clear();
wd.findElement(By.xpath("//form[@id='form']/div[2]/paper-input/paper-input-container/div[2]/div/input")).sendKeys("<PASSWORD>");
wd.findElement(By.xpath("//form[@id='form']//paper-button[.='login']")).click();
try { Thread.sleep(3000l); } catch (Exception e) { throw new RuntimeException(e); }
if(wd.findElement(By.tagName("html")).getText().contains("please login")){
System.out.println("Login failed");
throw ioe;
}//End of login
System.out.println("Login was executed successfully!");
System.out.println("Testcase finished successfully!");
wd.quit();
}
}
我想 运行 它就像在 katalon 中一样,但我不确定如何。
谢谢。
我尝试添加现有的 java 脚本而不声明 class 和 main 方法,它起作用了。 在您的示例中,请删除: import org.openqa.selenium.*; ,将其替换为: import org.openqa.selenium.By 然后粘贴剩余脚本 without
public class Login {
public static void main(String args[]) throws IOException {
}}
因此您在 Katalon 中的自定义测试用例将是:
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.By;
IOException ioe = new IOException();
//Initializing server
System.setProperty("webdriver.chrome.driver", "C:/selenium/chromedriver.exe");
ChromeDriver wd = new ChromeDriver();
wd.manage().window().maximize();
wd.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//login
System.out.println("*** login ***");
wd.get("<URL>");
wd.findElement(By.xpath("//form[@id='form']/div[1]/paper-input/paper-input-container/div[2]/div/input")).click();
wd.findElement(By.xpath("//form[@id='form']/div[1]/paper-input/paper-input-container/div[2]/div/input")).clear();
wd.findElement(By.xpath("//form[@id='form']/div[1]/paper-input/paper-input-container/div[2]/div/input")).sendKeys("<USERNAME>");
wd.findElement(By.xpath("//form[@id='form']/div[2]/paper-input/paper-input-container/div[2]/div/input")).click();
wd.findElement(By.xpath("//form[@id='form']/div[2]/paper-input/paper-input-container/div[2]/div/input")).clear();
wd.findElement(By.xpath("//form[@id='form']/div[2]/paper-input/paper-input-container/div[2]/div/input")).sendKeys("<PASSWORD>");
wd.findElement(By.xpath("//form[@id='form']//paper-button[.='login']")).click();
try { Thread.sleep(3000l); } catch (Exception e) { throw new RuntimeException(e); }
if(wd.findElement(By.tagName("html")).getText().contains("please login")){
System.out.println("Login failed");
throw ioe;
}//End of login
System.out.println("Login was executed successfully!");
System.out.println("Testcase finished successfully!");
wd.quit();