waitAnyKey
This commit is contained in:
44
second.ts
44
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 secondTest() {
|
||||
let driver: WebDriver | null = null;
|
||||
|
||||
try {
|
||||
|
||||
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();
|
||||
// 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();
|
||||
|
||||
// 创建一个 WebDriver 实例
|
||||
let driver = new Builder()
|
||||
driver = new Builder()
|
||||
.usingServer(seleniumServerUrl)
|
||||
.withCapabilities(capabilities)
|
||||
.build();
|
||||
|
||||
try {
|
||||
// 你的测试代码
|
||||
// 例如,打开 Google 网页
|
||||
await driver.get('http://www.google.com');
|
||||
|
||||
// 找到搜索框并输入查询词
|
||||
await driver.findElement(By.name('q')).sendKeys('Hello World', Key.RETURN);
|
||||
|
||||
// 等待搜索结果
|
||||
await driver.wait(until.titleContains('Hello World'), 1000);
|
||||
|
||||
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);
|
||||
}())
|
12
waitAnyKey.ts
Normal file
12
waitAnyKey.ts
Normal file
@ -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);
|
||||
});
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user