3

i got many errors about js... so i changed to chromedriver headless, and it work better for screenshot a specific element, but i got an errors in a other screenshot(s) code

Starting ChromeDriver 2.45.615291 (ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387) 

on port 44903
Only local connections are allowed.
janv. 31, 2019 7:02:22 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFOS: Detected dialect: OSS
Exception in thread "AWT-EventQueue-0" java.awt.image.RasterFormatException: (y + height) is outside of Raster
    at sun.awt.image.ByteInterleavedRaster.createWritableChild(Unknown Source)
    at java.awt.image.BufferedImage.getSubimage(Unknown Source)
    at CarteEtdInfo.photoProfile(CarteEtdInfo.java:57)
    at Accueil.<init>(Accueil.java:99)
    at Login$2.actionPerformed(Login.java:287)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.AbstractButton.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicRootPaneUI$Actions.actionPerformed(Unknown Source)
    at javax.swing.SwingUtilities.notifyAction(Unknown Source)
    at javax.swing.JComponent.processKeyBinding(Unknown Source)
    at javax.swing.KeyboardManager.fireBinding(Unknown Source)
    at javax.swing.KeyboardManager.fireKeyboardAction(Unknown Source)
    at javax.swing.JComponent.processKeyBindingsForAllComponents(Unknown Source)
    at javax.swing.JComponent.processKeyBindings(Unknown Source)
    at javax.swing.JComponent.processKeyEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

this is the the code that have errors from class CarteEtdInfo

public void photoProfile() throws IOException {

    String cookie = String.join("\n",Files.readAllLines(Paths.get("temp\\cookie.txt")));

    Login webpage = new Login();
    WebDriver pagee = webpage.driver;
    pagee.get("https://www4.inscription.tn/ORegMx/servlet/AuthentificationEtud?Idsession="+cookie+"&action1=toCarteEtd");


    // Get entire page screenshot
    WebElement taswira = 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]/div[2]/div[1]/table[2]/tbody[1]/tr[2]/td[1]/img[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 = taswira.getLocation();

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

    // Crop the entire page screenshot to get only element screenshot
    BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(), eleWidth, eleHeight); // line 57
    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\\avatar.png");
    try {
        FileUtils.copyFile(screenshot, screenshotLocation);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

this is the line 57

        BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(), eleWidth, eleHeight);

the preivious code of screenshot an element in Login class works fine, so why this one have a problem ?

also the chromedriver not headless works fine but the headless not

M J
  • 174
  • 1
  • 12
  • Apparently this is just an info message based off the comment linked and the test should run without any problems: [comment](https://groups.google.com/d/msg/selenium-users/WbmmA3aMrJM/9hbGp3-cAAAJ) – cullzie Jan 31 '19 at 06:25
  • i got those errors after i run the test – M J Jan 31 '19 at 06:27
  • it looks like there's a problem here `point.getX(), point.getY(), eleWidth, eleHeight` – M J Jan 31 '19 at 06:28
  • That's really weird, it work in not headless chrome but it didn't in headless chrome – M J Jan 31 '19 at 06:51

2 Answers2

2

Headless chrome default window size might be smaller than expected and this might cause your element to be outside of the window, hence not visible. You can either set the widndow size yourself or maximize window by using:

chromeOptions.addArguments(""--start-maximized")
Mate Mrše
  • 7,997
  • 10
  • 40
  • 77
  • You're right,thanks, i already post an answer btw,--start-maximized give me other errors, so i changed back to "window-size=1980,960" – M J Jan 31 '19 at 07:42
1

i fixed the problem by adding

chromeOptions.addArguments("window-size=1980,960");
M J
  • 174
  • 1
  • 12