在其他课程中使用相同的用户会话

时间:2019-01-27 22:55:47

标签: selenium selenium-webdriver cookies phantomjs session-cookies

我正在尝试找到在其他类中使用相同打开的phantomjs窗口/会话的方法。

如果窗口/会话被重新加载/重新打开/刷新,则用户将注销。

当我登录网站并输入我的帐户时,我按F5键或刷新,然后注销。

我上过课:登录/ CarteEtd / CarteEtd_Info / Accueil

当用户在Login类中的Jframe中登录时,它重定向到Accueil类(主页),当他/她单击它时它将找到图像/按钮,它将重定向到CarteEtd类,然后CarteEtd重定向到CarteEtd_Info < / p>

为什么选择CarteEtd类,然后选择CarteEtd_Info类?因为我不能继承CarteEtd中的Login类,因为CarteEtd已经扩展了JFrame,所以我将JFrame / GUI放在CarteEtd中,然后调用CarteEtd_Info,CarteEtd_Info类在打开的phantomjs中截取了特定用户的信息的屏幕快照。在登录类

现在的问题是,CarteEtd_Info使用Login类的驱动程序,但是它重新加载或打开了新的Phantomjs,idk,但我知道用户已注销

我正在使用屏幕截图获取验证码和用户身份证

这是一些登录类

    System.setProperty("phantomjs.binary.path", "phantomjs.exe");
    WebDriver driver = new PhantomJSDriver();

    //driver.manage().Window.Size = new Size(1920, 1080);
    driver.get("https://www4.inscription.tn/ORegMx/servlet/AuthentificationEtud?ident=cin");
    String title1=driver.getTitle();
    if (!(title1.equals("Site de l'inscription universitaire en ligne"))) {
        JOptionPane.showMessageDialog(null,"Verifiez votre connexion internet");
        driver.quit();

    }
    WebElement ele = driver.findElement(By.xpath("//td[@rowspan='2']"));

    driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
    // Get entire page screenshot
    File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    BufferedImage  fullImg = ImageIO.read(screenshot);

    // Get the location of element on the page
    org.openqa.selenium.Point point = ele.getLocation();

    // Get width and height of the element
    int eleWidth = ele.getSize().getWidth();
    int eleHeight = ele.getSize().getHeight();

    // Crop the entire page screenshot to get only element screenshot
    BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(),
        eleWidth, eleHeight);
    ImageIO.write(eleScreenshot, "png", screenshot);

    // Copy the element screenshot to disk
    File screenshotLocation = new File("temp\\captcha.png");
    FileUtils.copyFile(screenshot, screenshotLocation); 


    JLabel captcha = new JLabel("");
    captcha.setIcon(new ImageIcon("temp\\captcha.png"));
    captcha.setBounds(574, 304, 182, 50);
    frame.getContentPane().add(captcha);

这是CarteEtd中调用CarteEtd_Info的代码

    CarteEtd_Info id= new CarteEtd_Info();

    id.id();

这是CarteEtd_info类的所有代码

class CarteEtd_Info extends Login{



/**
 * 
 */
private static final long serialVersionUID = 1L;

public CarteEtd_Info() throws IOException {
    super();
    // TODO Auto-generated constructor stub
}

public void id() throws IOException {
//  driver= Login.driver;
    //WebDriver driver = sameWindow.driver;
    //driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
    WebElement ele = driver.findElement(By.xpath("/html[1]/body[1]/table[1]/tbody[1]/tr[1]/td[1]/table[1]/tbody[1]/tr[1]/td[1]/table[1]/tbody[1]/tr[1]/td[1]/form[1]/div[1]/table[1]/tbody[1]/tr[1]/td[3]/div[1]/span[1]/a[1]/i[1]"));
    driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
    //Actions builder = new Actions(driver);
   // builder.moveToElement(ele2).click(ele2);
   // builder.perform();
    //driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

    // Get entire page screenshot
    //WebElement ele = driver.findElement(By.xpath("/html[1]/body[1]/table[1]/tbody[1]/tr[1]/td[1]/table[1]/tbody[1]/tr[1]/td[1]/table[2]/tbody[1]/tr[4]/td[1]"));
    File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    BufferedImage fullImg = null;
    try {
        fullImg = ImageIO.read(screenshot);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // Get the location of element on the page
    org.openqa.selenium.Point point = ele.getLocation();

    // Get width and height of the element
    int eleWidth = ele.getSize().getWidth();
    int eleHeight = ele.getSize().getHeight();

    // Crop the entire page screenshot to get only element screenshot
    BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(),
        eleWidth, eleHeight);
    try {
        ImageIO.write(eleScreenshot, "png", screenshot);
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    // Copy the element screenshot to disk
    File screenshotLocation = new File("temp\\carte.png");
    try {
        FileUtils.copyFile(screenshot, screenshotLocation);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }   
}
}

我在想的是,是否有一种方法可以保存用户的cookie并在phantomjs新窗口中使用它

Login类中的代码捕获打开的phantomjs进程的pid,然后使用该pid调用该进程并在CarteEtd_Info中使用它

所有这些都是因为我想让我的代码干净后再作为我的学校项目在github中上载,所以在一堂课中使用所有内容都比较容易,但是我认为使用多堂课是专业的方式

0 个答案:

没有答案