From 84ca7b3fc322bec6ebdadda8891e49dba14fd690 Mon Sep 17 00:00:00 2001 From: Abdussamed Date: Sun, 19 Mar 2023 13:55:55 +0300 Subject: [PATCH] First commit --- .gitignore | 2 + Core/index.js | 200 ++++++++++++++++++++++++++++++++++++++++++++++ index.js | 49 ++++++++++++ package.json | 6 ++ projects/index.js | 5 ++ readme.md | 15 ++++ 6 files changed, 277 insertions(+) create mode 100644 .gitignore create mode 100644 Core/index.js create mode 100644 index.js create mode 100644 package.json create mode 100644 projects/index.js create mode 100644 readme.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..074ab52 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +package-lock.json +node_modules \ No newline at end of file diff --git a/Core/index.js b/Core/index.js new file mode 100644 index 0000000..4936b0c --- /dev/null +++ b/Core/index.js @@ -0,0 +1,200 @@ +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; \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..c21711a --- /dev/null +++ b/index.js @@ -0,0 +1,49 @@ +const fs = require("fs"); +const path = require("path"); +const { + createContext, + runInContext +} = require("vm"); + +const { + StartSelenium, + ClearElement, + ClickElement, + CreateTab, + GetAttribute, + SubmitElement, + WaitElement, + WriteElement, + Navigate, + goTab, + GoLastTab, + GoFirstTab, + NextTab, + PrevTab, + GetCurrentTabIndex +} = require("./Core/index"); + +async function runFile(file) +{ + let fileContext = fs.readFileSync(path.resolve(file),"utf-8"); + let context = createContext({ + StartSelenium, + ClearElement, + ClickElement, + CreateTab, + GetAttribute, + SubmitElement, + WaitElement, + WriteElement, + Navigate, + goTab, + GoLastTab, + GoFirstTab, + NextTab, + PrevTab, + GetCurrentTabIndex + }); + let scope = await runInContext(fileContext + '\n\nmain();', context); +} + +process.nextTick(runFile,"./projects/index.js"); \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..9e1a49b --- /dev/null +++ b/package.json @@ -0,0 +1,6 @@ +{ + "dependencies": { + "chromedriver": "^111.0.0", + "selenium-webdriver": "^4.8.1" + } +} diff --git a/projects/index.js b/projects/index.js new file mode 100644 index 0000000..47f4fe2 --- /dev/null +++ b/projects/index.js @@ -0,0 +1,5 @@ +async function main() +{ + await StartSelenium(); + await Navigate("https://argist.com/"); +}; \ No newline at end of file diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..4767ae3 --- /dev/null +++ b/readme.md @@ -0,0 +1,15 @@ +StartSelenium +ClickElement +WaitElement +ClearElement +WriteElement +GetAttribute +SubmitElement +CreateTab +Navigate +goTab +GoLastTab +GoFirstTab +NextTab +PrevTab +GetCurrentTabIndex \ No newline at end of file