Skip to content

Commit

Permalink
Merge pull request #97 from andresrc/JENKINS-34411
Browse files Browse the repository at this point in the history
[JENKINS-34411] Horizontal scrolling and scrolling overflows
  • Loading branch information
abayer committed May 9, 2016
2 parents 066f75f + ddc1dac commit 45d11e5
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 12 deletions.
59 changes: 55 additions & 4 deletions src/main/java/org/jenkinsci/test/acceptance/selenium/Scroller.java
@@ -1,7 +1,9 @@
package org.jenkinsci.test.acceptance.selenium;

import org.apache.commons.io.IOUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.events.AbstractWebDriverEventListener;
Expand Down Expand Up @@ -61,24 +63,73 @@
* @author Kohsuke Kawaguchi
*/
public class Scroller extends AbstractWebDriverEventListener {
private final String scrollJs;
/** Horizontal margin to use when scrolling to the element. */
private static final int MARGIN_X = Integer.getInteger("SCROLL_MARGIN_X", 200);
/** Vertical margin to use when scrolling to the element. */
private static final int MARGIN_Y = Integer.getInteger("SCROLL_MARGIN_Y", 200);

private final String overflowJS;

public Scroller() throws IOException {
scrollJs = IOUtils.toString(Scroller.class.getResourceAsStream("scroller.js"));
overflowJS = IOUtils.toString(Scroller.class.getResourceAsStream("overflow.js"));
}

@Override
public void beforeClickOn(WebElement element, WebDriver driver) {
scrollIntoView(element, driver);
}

@Override
public void afterClickOn(WebElement element, WebDriver driver) {
// A click can cause a page change.
overideOverflow(driver);
}

@Override
public void beforeChangeValueOf(WebElement element, WebDriver driver) {
scrollIntoView(element, driver);
}

@Override
public void afterNavigateTo(String url, WebDriver driver) {
overideOverflow(driver);
}

@Override
public void afterNavigateBack(WebDriver driver) {
overideOverflow(driver);
}

@Override
public void afterNavigateForward(WebDriver driver) {
overideOverflow(driver);
}

@Override
public void afterNavigateRefresh(WebDriver driver) {
overideOverflow(driver);
}

/**
* To scroll the element to the view, we scroll the element to the top-edge of the screen.
* A (configurable) margin is left to account for decorations.
* Alternatives based on `scrollToView` and {@link org.openqa.selenium.interactions.Actions#moveToElement}
* were tested but did not solve JENKINS-34411.
* @param e Element to scroll to view.
* @param driver Driver instance.
*/
private void scrollIntoView(WebElement e, WebDriver driver) {
int eYCoord = e.getLocation().getY();
((JavascriptExecutor)driver).executeScript(scrollJs, eYCoord);
final Point p = e.getLocation();
final int x = p.getX();
final int y = p.getY();
final String script = String.format("window.scrollTo(%d, %d);", p.getX() - MARGIN_X, p.getY() - MARGIN_Y);
((JavascriptExecutor)driver).executeScript(script);
}

/** Override overflow behavior (if not done previously). */
private void overideOverflow(WebDriver driver) {
if (driver.findElements(By.id("ath-overflow-override")).isEmpty()) {
((JavascriptExecutor)driver).executeScript(overflowJS);
}
}
}
@@ -0,0 +1,13 @@
var css = 'html body * { overflow: visible !important }',
head = document.head || document.getElementsByTagName('head')[0],
style = document.createElement('style');

style.type = 'text/css';
style.id = 'ath-overflow-override'
if (style.styleSheet){
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}

head.appendChild(style);

This file was deleted.

33 changes: 33 additions & 0 deletions src/test/java/plugins/MatrixAuthPluginTest.java
Expand Up @@ -10,6 +10,7 @@
import org.jenkinsci.test.acceptance.po.FreeStyleJob;
import org.jenkinsci.test.acceptance.po.GlobalSecurityConfig;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;

import static org.jenkinsci.test.acceptance.plugins.matrix_auth.MatrixRow.*;
import static org.junit.Assert.assertNotNull;
Expand Down Expand Up @@ -109,4 +110,36 @@ public void projectMatrixAuth() throws Exception {
jenkins.login().doLogin("bob");
assertNotNull(getElement(by.href("job/"+j.name+"/")));
}


/**
* In the context JENKINS-34411, the permissions out of view would not be activated,
* so the job creation would fail.
*/
@Test
@Issue("JENKINS-34411")
public void developer() throws Exception {
GlobalSecurityConfig sc = new GlobalSecurityConfig(jenkins);
sc.open();
{
MockSecurityRealm ms = sc.useRealm(MockSecurityRealm.class);
ms.configure("alice","bob");

MatrixAuthorizationStrategy mas = sc.useAuthorizationStrategy(MatrixAuthorizationStrategy.class);

MatrixRow a = mas.addUser("alice");
a.admin();

MatrixRow bob = mas.addUser("bob");
bob.developer();
}
sc.save();

jenkins.login().doLogin("bob");

// just create the job without configuring
FreeStyleJob j = jenkins.jobs.create();
jenkins.open();
j.open();
}
}

0 comments on commit 45d11e5

Please sign in to comment.