First commit

This commit is contained in:
Abdussamed 2023-03-19 13:55:55 +03:00
commit 84ca7b3fc3
6 changed files with 277 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
package-lock.json
node_modules

200
Core/index.js Normal file
View File

@ -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;

49
index.js Normal file
View File

@ -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");

6
package.json Normal file
View File

@ -0,0 +1,6 @@
{
"dependencies": {
"chromedriver": "^111.0.0",
"selenium-webdriver": "^4.8.1"
}
}

5
projects/index.js Normal file
View File

@ -0,0 +1,5 @@
async function main()
{
await StartSelenium();
await Navigate("https://argist.com/");
};

15
readme.md Normal file
View File

@ -0,0 +1,15 @@
StartSelenium
ClickElement
WaitElement
ClearElement
WriteElement
GetAttribute
SubmitElement
CreateTab
Navigate
goTab
GoLastTab
GoFirstTab
NextTab
PrevTab
GetCurrentTabIndex