200 lines
5.1 KiB
JavaScript
200 lines
5.1 KiB
JavaScript
const {
|
|
Builder,
|
|
By,
|
|
until,
|
|
WebDriver
|
|
} = require("selenium-webdriver");
|
|
const {Options} = require("selenium-webdriver/chrome");
|
|
require("chromedriver")
|
|
let sys = new Map();
|
|
sys.set("seleniumDrivers",[]);
|
|
sys.set("currentDriver",null);
|
|
|
|
|
|
async function StartSelenium()
|
|
{
|
|
let chromeOptions = new Options();
|
|
//chromeOptions.addArguments("--user-data-dir", )
|
|
const browser = await new Builder().setChromeOptions(chromeOptions).forBrowser("chrome").build();
|
|
sys.get("seleniumDrivers").push(browser);
|
|
sys.set("currentDriver", browser);
|
|
}
|
|
|
|
async function CreateTab()
|
|
{
|
|
/**@type {WebDriver} */
|
|
let tab = sys.get("currentDriver");
|
|
await tab.switchTo().newWindow();
|
|
await tab.get("about:blank");
|
|
return tab.getWindowHandle();
|
|
}
|
|
|
|
async function goTab(index)
|
|
{
|
|
/**@type {WebDriver} */
|
|
let tab = sys.get("currentDriver");
|
|
if(typeof index == "number")
|
|
{
|
|
let windows = await tab.getAllWindowHandles();
|
|
await tab.switchTo().window(windows[index]);
|
|
}else{
|
|
await tab.switchTo().window(index);
|
|
}
|
|
}
|
|
async function GoLastTab()
|
|
{
|
|
/**@type {WebDriver} */
|
|
let tab = sys.get("currentDriver");
|
|
let windows = await tab.getAllWindowHandles();
|
|
await tab.switchTo().window(windows[windows.length - 1]);
|
|
}
|
|
async function GoFirstTab()
|
|
{
|
|
/**@type {WebDriver} */
|
|
let tab = sys.get("currentDriver");
|
|
let windows = await tab.getAllWindowHandles();
|
|
await tab.switchTo().window(windows[0]);
|
|
}
|
|
async function NextTab()
|
|
{
|
|
let tab = sys.get("currentDriver");
|
|
let windows = await tab.getAllWindowHandles();
|
|
let index = await GetCurrentTabIndex();
|
|
if(index == windows.length - 1)
|
|
{
|
|
return;
|
|
}
|
|
await tab.switchTo().window(windows[index + 1]);
|
|
}
|
|
async function PrevTab()
|
|
{
|
|
let tab = sys.get("currentDriver");
|
|
let windows = await tab.getAllWindowHandles();
|
|
let index = await GetCurrentTabIndex();
|
|
if(index == 0)
|
|
{
|
|
return;
|
|
}
|
|
await tab.switchTo().window(windows[index - 1]);
|
|
}
|
|
async function GetCurrentTabIndex()
|
|
{
|
|
/**@type {WebDriver} */
|
|
let tab = sys.get("currentDriver");
|
|
let window = await tab.getWindowHandle();
|
|
let windows = await tab.getAllWindowHandles();
|
|
let index = 0;
|
|
for(;index < windows.length; index++)
|
|
{
|
|
let key = windows[index];
|
|
if(window == key)
|
|
{
|
|
return index;
|
|
}
|
|
}
|
|
}
|
|
|
|
async function WaitElement(selector, timeout = 3000)
|
|
{
|
|
/**@type {WebDriver} */
|
|
let tab = sys.get("currentDriver");
|
|
await tab.wait(
|
|
until.elementLocated(By.css(selector)),
|
|
timeout
|
|
);
|
|
}
|
|
|
|
async function ClickElement(selector,checkLocated = true, timeout = 3000)
|
|
{
|
|
/**@type {WebDriver} */
|
|
let tab = sys.get("currentDriver");
|
|
if(checkLocated)
|
|
{
|
|
WaitElemen(selector, timeout)
|
|
};
|
|
let element = await tab.findElement(By.css(selector));
|
|
await element.click();
|
|
}
|
|
async function ClearElement(selector,checkLocated = true, timeout = 3000)
|
|
{
|
|
/**@type {WebDriver} */
|
|
let tab = sys.get("currentDriver");
|
|
if(checkLocated)
|
|
{
|
|
WaitElemen(selector, timeout)
|
|
};
|
|
let element = await tab.findElement(By.css(selector));
|
|
await element.clear();
|
|
}
|
|
|
|
async function WriteElement(selector, value,checkLocated = true, timeout = 3000)
|
|
{
|
|
/**@type {WebDriver} */
|
|
let tab = sys.get("currentDriver");
|
|
if(checkLocated)
|
|
{
|
|
WaitElemen(selector, timeout)
|
|
};
|
|
let element = await tab.findElement(By.css(selector));
|
|
await element.sendKeys(value);
|
|
}
|
|
|
|
async function GetAttribute(selector, name, checkLocated = true, timeout = 3000)
|
|
{
|
|
/**@type {WebDriver} */
|
|
let tab = sys.get("currentDriver");
|
|
if(checkLocated)
|
|
{
|
|
WaitElemen(selector, timeout)
|
|
};
|
|
let element = await tab.findElement(By.css(selector));
|
|
return await element.getAttribute(name)
|
|
}
|
|
|
|
async function GetAttribute(selector, name, checkLocated = true, timeout = 3000)
|
|
{
|
|
/**@type {WebDriver} */
|
|
let tab = sys.get("currentDriver");
|
|
if(checkLocated)
|
|
{
|
|
WaitElemen(selector, timeout)
|
|
};
|
|
let element = await tab.findElement(By.css(selector));
|
|
return await element.getAttribute(name)
|
|
}
|
|
|
|
async function SubmitElement(selector, checkLocated = true, timeout = 3000)
|
|
{
|
|
/**@type {WebDriver} */
|
|
let tab = sys.get("currentDriver");
|
|
if(checkLocated)
|
|
{
|
|
WaitElemen(selector, timeout)
|
|
};
|
|
let element = await tab.findElement(By.css(selector));
|
|
return await element.submit()
|
|
}
|
|
|
|
async function Navigate(url)
|
|
{
|
|
/**@type {WebDriver} */
|
|
let tab = sys.get("currentDriver");
|
|
await tab.get(url);
|
|
}
|
|
|
|
exports.sys = sys;
|
|
exports.StartSelenium = StartSelenium;
|
|
exports.ClickElement = ClickElement;
|
|
exports.WaitElement = WaitElement;
|
|
exports.ClearElement = ClearElement;
|
|
exports.WriteElement = WriteElement;
|
|
exports.GetAttribute = GetAttribute;
|
|
exports.SubmitElement = SubmitElement;
|
|
exports.CreateTab = CreateTab;
|
|
exports.Navigate = Navigate;
|
|
exports.goTab = goTab;
|
|
exports.GoLastTab = GoLastTab;
|
|
exports.GoFirstTab = GoFirstTab;
|
|
exports.NextTab = NextTab;
|
|
exports.PrevTab = PrevTab;
|
|
exports.GetCurrentTabIndex = GetCurrentTabIndex; |