0

I was trying to import a JavaScript file to my server side JavaScript file because I can't run the function on my server side. Is there any way to import my src to server side js so that I can run the function. Thanks in advance.

JS file to export to server side

const {Builder, By, Key, util} = require("selenium-webdriver");

(async function googlesearch() {
    let driver = await new Builder().forBrowser('chrome').build();
    try{
        await driver.get("https://www.google.com/");
        let searchField = await driver.findElement(By.name('q'));
        await searchField.sendKeys("workout videos");
        await searchField.sendKeys(Key.ENTER);
        await driver.wait(until.titleIs('workout videos - Google Search'), 5000);

        let list = await driver.findElements(By.className('g'));
        if(list.length <= 0 ){
            throw new Error('Test - FAIL');
        }else{
            console.log('Test - Success');
        }
    } finally {
        console.log("automation complete")
    }
    
})();

sarem dave
  • 97
  • 2
  • 8

2 Answers2

0

i think you could something like

const {Builder, By, Key, util} = require("selenium-webdriver");

exports.googlesearch = async () => {
  let driver = await new Builder().forBrowser('chrome').build();
    try{
        await driver.get("https://www.google.com/");
        let searchField = await driver.findElement(By.name('q'));
        await searchField.sendKeys("workout videos");
        await searchField.sendKeys(Key.ENTER);
        await driver.wait(until.titleIs('workout videos - Google Search'), 5000);

        let list = await driver.findElements(By.className('g'));
        if(list.length <= 0 ){
            throw new Error('Test - FAIL');
        }else{
            console.log('Test - Success');
        }
    } finally {
        console.log("automation complete")
    }
}

so you can import as

const { googlesearch } = require('./file/path')
0

Use module.exports in the file you want to import using Commonjs modules.

For example

const {Builder, By, Key, util} = require("selenium-webdriver");

async function googlesearch() {
    let driver = await new Builder().forBrowser('chrome').build();
    try{
        await driver.get("https://www.google.com/");
        let searchField = await driver.findElement(By.name('q'));
        await searchField.sendKeys("workout videos");
        await searchField.sendKeys(Key.ENTER);
        await driver.wait(until.titleIs('workout videos - Google Search'), 5000);

        let list = await driver.findElements(By.className('g'));
        if(list.length <= 0 ){
            throw new Error('Test - FAIL');
        }else{
            console.log('Test - Success');
        }
    } finally {
        console.log("automation complete")
    }
    
});

// this was added here:
module.exports.googlesearch = googlesearch;

Then import/require it like this:

const {googlesearch} = require("./yourfilename");

or if you're using js modules export it like this:

export googlesearch;

And to import:

import {googlesearch} from "./yourfilename";
Tembero
  • 387
  • 3
  • 11