waitAnyKey
This commit is contained in:
48
second.ts
48
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() {
|
(async function secondTest() {
|
||||||
// Selenium Server URL (Assuming your Selenium Server is running on localhost at port 4444)
|
let driver: WebDriver | null = null;
|
||||||
const seleniumServerUrl = "http://localhost:4444/wd/hub";
|
|
||||||
|
|
||||||
// 使用 Chrome 浏览器
|
|
||||||
const capabilities = Capabilities.chrome();
|
|
||||||
|
|
||||||
// 创建一个 WebDriver 实例
|
|
||||||
let driver = new Builder()
|
|
||||||
.usingServer(seleniumServerUrl)
|
|
||||||
.withCapabilities(capabilities)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 你的测试代码
|
|
||||||
// 例如,打开 Google 网页
|
|
||||||
await driver.get('http://www.google.com');
|
|
||||||
|
|
||||||
// 找到搜索框并输入查询词
|
// Selenium Server URL (Assuming your Selenium Server is running on localhost at port 4444)
|
||||||
await driver.findElement(By.name('q')).sendKeys('Hello World', Key.RETURN);
|
// 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 实例
|
||||||
await driver.wait(until.titleContains('Hello World'), 1000);
|
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 {
|
} finally {
|
||||||
// 关闭浏览器
|
await new Promise(resolve => setTimeout(resolve, 5 * 1000));
|
||||||
await driver.quit();
|
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