diff --git a/second.ts b/second.ts index a9f6c52..79c1dbb 100644 --- a/second.ts +++ b/second.ts @@ -1,33 +1,31 @@ -import { Builder, By, Capabilities, Key, until } from 'selenium-webdriver'; +import { Builder, By, Capabilities, WebDriver } from 'selenium-webdriver'; +import waitAnyKey from './waitAnyKey'; -async function runTests() { - // Selenium Server URL (Assuming your Selenium Server is running on localhost at port 4444) - const seleniumServerUrl = "http://localhost:4444/wd/hub"; - - // 使用 Chrome 浏览器 - const capabilities = Capabilities.chrome(); - - // 创建一个 WebDriver 实例 - let driver = new Builder() - .usingServer(seleniumServerUrl) - .withCapabilities(capabilities) - .build(); +(async function secondTest() { + let driver: WebDriver | null = null; try { - // 你的测试代码 - // 例如,打开 Google 网页 - await driver.get('http://www.google.com'); - // 找到搜索框并输入查询词 - await driver.findElement(By.name('q')).sendKeys('Hello World', Key.RETURN); + // Selenium Server URL (Assuming your Selenium Server is running on localhost at port 4444) + // http://150.230.239.12:7900/?autoconnect=1&resize=scale&password=secret + const seleniumServerUrl = "http://150.230.239.12:4444/wd/hub"; + const capabilities = Capabilities.firefox(); - // 等待搜索结果 - await driver.wait(until.titleContains('Hello World'), 1000); + // 创建一个 WebDriver 实例 + driver = new Builder() + .usingServer(seleniumServerUrl) + .withCapabilities(capabilities) + .build(); + await driver.get("https://www.baidu.com/") + // await driver.manage().window().setSize(1176, 800) + await driver.findElement(By.id("kw")).sendKeys("测试") + await driver.findElement(By.id("su")).click() + } catch (e) { + console.log(e) } finally { - // 关闭浏览器 - await driver.quit(); + await new Promise(resolve => setTimeout(resolve, 5 * 1000)); + waitAnyKey(); + await driver?.quit(); } -} - -runTests().catch(console.error); +}()) \ No newline at end of file diff --git a/waitAnyKey.ts b/waitAnyKey.ts new file mode 100644 index 0000000..ac7738b --- /dev/null +++ b/waitAnyKey.ts @@ -0,0 +1,12 @@ +export default function waitAnyKey() { + console.log('Press any key to continue...'); + return new Promise(resolve => { + process.stdin.setRawMode(true); + process.stdin.resume(); + process.stdin.once('data', () => { + process.stdin.setRawMode(false); + process.stdin.pause(); // 在这里添加 + resolve(undefined); + }); + }); +}