使用selenium IDE插件直接生成Java代码

tech2023-05-29  105

首先安装Selenium IDE插件

而是配置Selenium环境

生成的代码

// Generated by Selenium IDE import org.junit.Test; import org.junit.Before; import org.junit.After; import static org.junit.Assert.*; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.core.IsNot.not; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.remote.RemoteWebDriver; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.Dimension; import org.openqa.selenium.WebElement; import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.Alert; import org.openqa.selenium.Keys; import java.util.*; import java.net.MalformedURLException; import java.net.URL; public class 1Test {   private WebDriver driver;   private Map<String, Object> vars;   JavascriptExecutor js;   @Before   public void setUp() {     driver = new ChromeDriver();     js = (JavascriptExecutor) driver;     vars = new HashMap<String, Object>();   }   @After   public void tearDown() {     driver.quit();   }   public String waitForWindow(int timeout) {     try {       Thread.sleep(timeout);     } catch (InterruptedException e) {       e.printStackTrace();     }     Set<String> whNow = driver.getWindowHandles();     Set<String> whThen = (Set<String>) vars.get("window_handles");     if (whNow.size() > whThen.size()) {       whNow.removeAll(whThen);     }     return whNow.iterator().next();   }   @Test   public void 1() {     driver.get("https://XXXXXXXXXXXXX/");     driver.manage().window().setSize(new Dimension(945, 1020));     vars.put("window_handles", driver.getWindowHandles());     driver.findElement(By.linkText("XXXXXXXXXXX")).click();     vars.put("win6315", waitForWindow(2000));     driver.switchTo().window(vars.get("win6315").toString());     vars.put("window_handles", driver.getWindowHandles());     driver.findElement(By.linkText("XXXXXXXXXXXXXX")).click();     vars.put("win5391", waitForWindow(2000));     driver.switchTo().window(vars.get("win5391").toString());   } }  

 

最新回复(0)