使用无头浏览器在Selenium Java AWS Lambda中运行测试

时间:2020-07-01 11:36:33

标签: java amazon-web-services selenium google-chrome-headless

嗨,我正在尝试在Selenium java中运行以下脚本:

package CPP_Test;

import java.io.File;
import java.io.IOException;
import java.time.Duration;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

public class PythonCAGEN1 implements RequestHandler<Object, Object>{
    
    private WebDriver driver;
    private String baseUrl;
    Actions actions;
    String email;

    String code = "list1 = [int(n) for n in raw_input().split(\" \")]\r\n" + 
            "list2 = [int(n) for n in raw_input().split(\" \")]\r\n" +
            "\n"+
            "list1.sort()\n" + 
            "list2.sort()\n" + 
            "\n" + 
            "if cmp(list1, list2) == 0:\n" + 
            "   print \"YES\"" + 
            "\n\b\b\b\belse:\n" + 
            "   print \"NO\"\n" + 
            " ";

    @Before
    public void setUp() throws Exception {

        //System.setProperty("webdriver.chrome.driver", "./resources/chromedriver.exe");  
        
          ChromeOptions options= new ChromeOptions();
          options.addArguments("window-size=1400,800");
          options.addArguments("headless");
         
         
        driver = new ChromeDriver(options);
        baseUrl = "https://codeassess-codeassess-dev.azurewebsites.net/drive.aspx?id=CAD471756";
       // baseUrl = "https://codeassess-codeassess-dev.azurewebsites.net/drive.aspx?id=CAD471756";
    driver.manage().window().maximize();
    driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }

    @Test
    public void testCAGEN1CPP() throws Exception {

    driver.get(baseUrl);
    actions = new Actions(driver);

    WebDriverWait wait= new WebDriverWait(driver, 10);
    email = "test" + System.currentTimeMillis() + "@testmail.com";

    driver.findElement(By.id("formid1")).sendKeys("test" + System.currentTimeMillis());
    //driver.findElement(By.xpath("//div[@id='divcustomform2']/table/tbody/tr/td")).click();
    System.out.println("entered username");

    driver.findElement(By.id("formid2")).sendKeys(email);
    System.out.println("entered email");

    driver.findElement(By.id("formid3")).sendKeys("123456");
    driver.findElement(By.id("btnsubmitform")).click();

    driver.findElement(By.id("ContentPlaceHolder1_txtLsAttend")).sendKeys("hansraj");

    driver.findElement(By.id("ContentPlaceHolder1_txtDegree")).sendKeys("btech");
    driver.findElement(By.id("ContentPlaceHolder1_txtfoStudy")).sendKeys("computer science");

    driver.findElement(By.id("ContentPlaceHolder1_btnstart")).click();
    driver.findElement(By.id("ContentPlaceHolder1_ImageButton1")).click();

    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    WebElement skipButton=driver.findElement(By.xpath("(//div[@class='contain help-btns-container']//a[@onclick='skipme();'])[1]"));
    if (skipButton.isDisplayed()) {
    Actions act = new Actions(driver);
    act.click(skipButton).build().perform();
    }
    else {

    System.out.println("Skip not present");
    }
    driver.findElement(By.id("ContentPlaceHolder1_drplangchoice")).click();
    new Select(driver.findElement(By.id("ContentPlaceHolder1_drplangchoice"))).selectByVisibleText("PYTHON 2.7");
    driver.findElement(By.id("ContentPlaceHolder1_drplangchoice")).click();

    System.out.println("Selected C++");

    WebElement txtbox = driver.findElement(By.xpath("//div[@class='CodeMirror-lines']"));
    new WebDriverWait(driver, 3);
    actions.click(txtbox).keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).sendKeys(Keys.BACK_SPACE).build()
    .perform();

    System.out.println("successfully cleared textbox");
    new WebDriverWait(driver, 3);
    doActionsSendKeys(txtbox, code);
    System.out.println("code entered successfully");
    driver.findElement(By.xpath("//*[@id='ContentPlaceHolder1_txtsubmitsol']"));
    driver.findElement(By.xpath("//input[@value='Submit Your Code']")).click();
    System.out.println("Code submitted successfully");
    WebElement correct_sol=driver.findElement(By.xpath("//div[@id='tdpostoutout']/span[contains(text(),'Submitted Solution is Correct!')]"));
    
     wait.until(ExpectedConditions.visibilityOf(correct_sol));
      String Solution_Text= correct_sol.getText();
    if(Solution_Text.equalsIgnoreCase("Submitted Solution is Correct!")) {
        System.out.println("Successfully compiled the code");
    }
    TakesScreenshot ts= (TakesScreenshot)driver;
    
    try {
    File source= ts.getScreenshotAs(OutputType.FILE);
    String dest= "D:/Codeaccess/Hackathon/CAGEN1/Python_Screenshot/Successful_Submission"+System.currentTimeMillis()+".png";
    File destination= new File(dest);
      FileUtils.copyFile(source, destination);
      System.out.println("Screenshot captured");
    }
    catch (IOException e) {
        // TODO: handle exception
        e.printStackTrace();
    }
    

      new WebDriverWait(driver, 5);
      WebElement endTestButton =driver.findElement(By.xpath("//a[text()='End Test']"));
      javascriptExecutorClick(driver, endTestButton);
      
      
      driver.findElement(By.xpath("(//a[contains(text(),'Yes')])[5]")).click();
      
      System.out.println("clicked on end test");
      
      driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
      driver.findElement(By.id("ContentPlaceHolder1_btnfinishtest")).click();
     
    }


    public void doActionsSendKeys(WebElement element, String value) {
    Actions actions = new Actions(driver);
    actions.sendKeys(element, value).build().perform();
    }

    
      public void doActionsMoveToElement(WebElement element) { 
          Actions actions =  new Actions(driver); 
          actions.moveToElement((element)).build().perform();
      
      }
     
    public static void javascriptExecutorClick(WebDriver driver, WebElement element) {
    JavascriptExecutor executor = (JavascriptExecutor) driver;
    executor.executeScript("arguments[0].click();", element);
    }

    public Object handleRequest(Object input, Context context) {
        // TODO Auto-generated method stub
        return null;
    }

}

1)我需要对Google chrome无头浏览器进行哪些配置才能在AWS Lambda中运行它?

2)目前我的屏幕截图也正在保存在本地计算机上,如何将它们保存在S3存储桶中?

我还需要其他配置吗?

0 个答案:

没有答案