Skip to content

Commit

Permalink
replace sendkeys with javascript function
Browse files Browse the repository at this point in the history
  • Loading branch information
prabhjyotsingh committed Mar 18, 2016
1 parent 734a901 commit 0249b49
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ protected void sleep(long millis, boolean logOutput) {
}
}

protected void setTextOfParagraph(int paragraphNo, String text) {
String editorId = driver.findElement(By.xpath(getParagraphXPath(paragraphNo) + "//div[contains(@class, 'editor')]")).getAttribute("id");
if (driver instanceof JavascriptExecutor) {
((JavascriptExecutor) driver).executeScript("ace.edit('" + editorId + "'). setValue('" + text + "')");
} else {
throw new IllegalStateException("This driver does not support JavaScript!");
}
}

protected void runParagraph(int paragraphNo) {
driver.findElement(By.xpath(getParagraphXPath(paragraphNo) + "//span[@class='icon-control-play']")).click();
}


protected String getParagraphXPath(int paragraphNo) {
return "//div[@ng-controller=\"ParagraphCtrl\"][" + paragraphNo + "]";
Expand Down Expand Up @@ -136,40 +149,6 @@ protected void deleteTestNotebook(final WebDriver driver) {
sleep(100, true);
}

public enum HelperKeys implements CharSequence {
OPEN_PARENTHESIS(Keys.chord(Keys.SHIFT, "9")),
EXCLAMATION(Keys.chord(Keys.SHIFT, "1")),
PERCENTAGE(Keys.chord(Keys.SHIFT, "5")),
SHIFT_ENTER(Keys.chord(SHIFT, ENTER));

private final CharSequence keyCode;

HelperKeys(CharSequence keyCode) {
this.keyCode = keyCode;
}

public char charAt(int index) {
return index == 0 ? keyCode.charAt(index) : '\ue000';
}

public int length() {
return 1;
}

public CharSequence subSequence(int start, int end) {
if (start == 0 && end == 1) {
return String.valueOf(this.keyCode);
} else {
throw new IndexOutOfBoundsException();
}
}

public String toString() {
return String.valueOf(this.keyCode);
}

}

protected void handleException(String message, Exception e) throws Exception {
LOG.error(message, e);
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ public void testCreateNewButton() throws Exception {
"//div[@class='modal-footer']//button[contains(.,'OK')]")).click();
ZeppelinITUtils.sleep(1000, false);

WebElement oldParagraphEditor = driver.findElement(By.xpath(getParagraphXPath(1) + "//textarea"));
oldParagraphEditor.sendKeys(" original paragraph ");
setTextOfParagraph(1, " original paragraph ");

WebElement newPara = driver.findElement(By.xpath(getParagraphXPath(1) + "//div[contains(@class,'new-paragraph')][1]"));
action.moveToElement(newPara).click().build().perform();
Expand All @@ -98,8 +97,7 @@ public void testCreateNewButton() throws Exception {
collector.checkThat("Paragraph is created above",
driver.findElement(By.xpath(getParagraphXPath(1) + "//div[contains(@class, 'editor')]")).getText(),
CoreMatchers.equalTo(""));
WebElement aboveParagraphEditor = driver.findElement(By.xpath(getParagraphXPath(1) + "//textarea"));
aboveParagraphEditor.sendKeys(" this is above ");
setTextOfParagraph(1, " this is above ");

newPara = driver.findElement(By.xpath(getParagraphXPath(2) + "//div[contains(@class,'new-paragraph')][2]"));
action.moveToElement(newPara).click().build().perform();
Expand All @@ -109,8 +107,7 @@ public void testCreateNewButton() throws Exception {
collector.checkThat("Paragraph is created below",
driver.findElement(By.xpath(getParagraphXPath(3) + "//div[contains(@class, 'editor')]")).getText(),
CoreMatchers.equalTo(""));
WebElement belowParagraphEditor = driver.findElement(By.xpath(getParagraphXPath(3) + "//textarea"));
belowParagraphEditor.sendKeys(" this is below ");
setTextOfParagraph(3, " this is below ");

collector.checkThat("The output field of paragraph1 contains",
driver.findElement(By.xpath(getParagraphXPath(1) + "//div[contains(@class, 'editor')]")).getText(),
Expand Down Expand Up @@ -176,16 +173,14 @@ public void testMoveUpAndDown() throws Exception {
createNewNote();

waitForParagraph(1, "READY");
WebElement paragraph1Editor = driver.findElement(By.xpath(getParagraphXPath(1) + "//textarea"));
paragraph1Editor.sendKeys("1");
setTextOfParagraph(1, "1");

driver.findElement(By.xpath(getParagraphXPath(1) + "//span[@class='icon-settings']")).click();
driver.findElement(By.xpath(getParagraphXPath(1) + "//ul/li/a[@ng-click='insertNew()']")).click();


waitForParagraph(2, "READY");
WebElement paragraph2Editor = driver.findElement(By.xpath(getParagraphXPath(2) + "//textarea"));
paragraph2Editor.sendKeys("2");
setTextOfParagraph(2, "2");


collector.checkThat("The paragraph1 value contains",
Expand Down Expand Up @@ -236,9 +231,7 @@ public void testDisableParagraphRunButton() throws Exception {
createNewNote();

waitForParagraph(1, "READY");
WebElement paragraph1Editor = driver.findElement(By.xpath(getParagraphXPath(1) + "//textarea"));
paragraph1Editor.sendKeys("println" + Keys.chord(Keys.SHIFT, "9") + "\""
+ "abcd\")");
setTextOfParagraph(1, "println (\"abcd\")");

driver.findElement(By.xpath(getParagraphXPath(1) + "//span[@class='icon-settings']")).click();
driver.findElement(By.xpath(getParagraphXPath(1) + "//ul/li/a[@ng-click='toggleEnableDisable()']")).click();
Expand Down Expand Up @@ -275,9 +268,7 @@ public void testClearOutputButton() throws Exception {

waitForParagraph(1, "READY");
String xpathToOutputField=getParagraphXPath(1) + "//div[contains(@ng-if,'getResultType()')]";
WebElement paragraph1Editor = driver.findElement(By.xpath(getParagraphXPath(1) + "//textarea"));
paragraph1Editor.sendKeys("println" + Keys.chord(Keys.SHIFT, "9") + "\""
+ "abcd\")");
setTextOfParagraph(1, "println (\"abcd\")");
collector.checkThat("Before Run Output field contains ",
driver.findElement(By.xpath(xpathToOutputField)).getText(),
CoreMatchers.equalTo(""));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.apache.zeppelin.AbstractZeppelinIT.HelperKeys.*;
import static org.openqa.selenium.Keys.*;

public class SparkParagraphIT extends AbstractZeppelinIT {
private static final Logger LOG = LoggerFactory.getLogger(SparkParagraphIT.class);

Expand Down Expand Up @@ -67,17 +64,10 @@ public void testSpark() throws Exception {
return;
}
try {
WebElement paragraph1Editor = driver.findElement(By.xpath(getParagraphXPath(1) + "//textarea"));
paragraph1Editor.sendKeys("sc.version");
paragraph1Editor.sendKeys(SHIFT_ENTER);
setTextOfParagraph(1, "sc.version");
runParagraph(1);

waitForParagraph(1, "FINISHED");
WebElement paragraph1Result = driver.findElement(By.xpath(
getParagraphXPath(1) + "//div[@class=\"tableDisplay\"]"));
Float sparkVersion = Float.parseFloat(paragraph1Result.getText().split("= ")[1].substring(0, 3));

WebElement paragraph2Editor = driver.findElement(By.xpath(getParagraphXPath(2) + "//textarea"));


/*
equivalent of
Expand All @@ -90,50 +80,15 @@ case class Bank(age: Integer, job: String, marital: String, education: String, b
val bank = bankText.map(s => s.split(";")).filter(s => s(0) != "\"age\"").map(s => Bank(s(0).toInt,s(1).replaceAll("\"", ""),s(2).replaceAll("\"", ""),s(3).replaceAll("\"", ""),s(5).replaceAll("\"", "").toInt)).toDF()
bank.registerTempTable("bank")
*/
paragraph2Editor.sendKeys("import org.apache.commons.io.IOUtils" +
ENTER +

"import java.net.URL" +
ENTER +

"import java.nio.charset.Charset" +
ENTER +

"val bankText = sc.parallelize" + OPEN_PARENTHESIS +
"IOUtils.toString" + OPEN_PARENTHESIS + "new URL" + OPEN_PARENTHESIS
+ "\"https://s3.amazonaws.com/apache" + SUBTRACT + "zeppelin/tutorial/bank/bank." +
"csv\"),Charset.forName" + OPEN_PARENTHESIS + "\"utf8\"))" +
".split" + OPEN_PARENTHESIS + "\"\\n\"))" +
ENTER +

"case class Bank" + OPEN_PARENTHESIS +
"age: Integer, job: String, marital: String, education: String, balance: Integer)" +
ENTER +
ENTER +

"val bank = bankText.map" + OPEN_PARENTHESIS + "s => s.split" +
OPEN_PARENTHESIS + "\";\")).filter" + OPEN_PARENTHESIS +
"s => s" + OPEN_PARENTHESIS + "0) " + EXCLAMATION +
"= \"\\\"age\\\"\").map" + OPEN_PARENTHESIS +
"s => Bank" + OPEN_PARENTHESIS + "s" + OPEN_PARENTHESIS +
"0).toInt,s" + OPEN_PARENTHESIS + "1).replaceAll" +
OPEN_PARENTHESIS + "\"\\\"\", \"\")," +
"s" + OPEN_PARENTHESIS + "2).replaceAll" +
OPEN_PARENTHESIS + "\"\\\"\", \"\")," +
"s" + OPEN_PARENTHESIS + "3).replaceAll" +
OPEN_PARENTHESIS + "\"\\\"\", \"\")," +
"s" + OPEN_PARENTHESIS + "5).replaceAll" +
OPEN_PARENTHESIS + "\"\\\"\", \"\").toInt" + ")" +
")" + (sparkVersion < 1.3f ? "" : ".toDF" + OPEN_PARENTHESIS + ")") +
ENTER +

"bank.registerTempTable" + OPEN_PARENTHESIS + "\"bank\")"
);
paragraph2Editor.sendKeys("" + END + BACK_SPACE + BACK_SPACE +
BACK_SPACE + BACK_SPACE + BACK_SPACE + BACK_SPACE +
BACK_SPACE + BACK_SPACE + BACK_SPACE + BACK_SPACE + BACK_SPACE);

paragraph2Editor.sendKeys(SHIFT_ENTER);
setTextOfParagraph(2, "import org.apache.commons.io.IOUtils\\n" +
"import java.net.URL\\n" +
"import java.nio.charset.Charset\\n" +
"val bankText = sc.parallelize(IOUtils.toString(new URL(\"https://s3.amazonaws.com/apache-zeppelin/tutorial/bank/bank.csv\"),Charset.forName(\"utf8\")).split(\"\\\\n\"))\\n" +
"case class Bank(age: Integer, job: String, marital: String, education: String, balance: Integer)\\n" +
"\\n" +
"val bank = bankText.map(s => s.split(\";\")).filter(s => s(0) != \"\\\\\"age\\\\\"\").map(s => Bank(s(0).toInt,s(1).replaceAll(\"\\\\\"\", \"\"),s(2).replaceAll(\"\\\\\"\", \"\"),s(3).replaceAll(\"\\\\\"\", \"\"),s(5).replaceAll(\"\\\\\"\", \"\").toInt)).toDF()\\n" +
"bank.registerTempTable(\"bank\")");
runParagraph(2);

try {
waitForParagraph(2, "FINISHED");
Expand Down Expand Up @@ -164,14 +119,11 @@ public void testPySpark() throws Exception {
return;
}
try {
WebElement paragraph1Editor = driver.findElement(By.xpath(getParagraphXPath(1) + "//textarea"));

paragraph1Editor.sendKeys(PERCENTAGE + "pyspark" + ENTER +
"for x in range" + OPEN_PARENTHESIS + "0, 3):" + ENTER +
" print \"test loop " + PERCENTAGE + "d\" " +
PERCENTAGE + " " + OPEN_PARENTHESIS + "x)" + ENTER);
setTextOfParagraph(1, "%pyspark\\n" +
"for x in range(0, 3):\\n" +
" print \"test loop %d\" % (x)");

paragraph1Editor.sendKeys(SHIFT_ENTER);
runParagraph(1);

try {
waitForParagraph(1, "FINISHED");
Expand Down Expand Up @@ -199,12 +151,9 @@ public void testSqlSpark() throws Exception {
return;
}
try {
WebElement paragraph1Editor = driver.findElement(By.xpath(getParagraphXPath(1) + "//textarea"));

paragraph1Editor.sendKeys(PERCENTAGE + "sql" + ENTER +
setTextOfParagraph(1,"%sql\\n" +
"select * from bank limit 1");

paragraph1Editor.sendKeys(SHIFT_ENTER);
runParagraph(1);

try {
waitForParagraph(1, "FINISHED");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,8 @@ public void testAngularDisplay() throws Exception {
* print angular template
* %angular <div id='angularTestButton' ng-click='myVar=myVar+1'>BindingTest_{{myVar}}_</div>
*/
WebElement paragraph1Editor = driver.findElement(By.xpath(getParagraphXPath(1) + "//textarea"));
paragraph1Editor.sendKeys("println" + Keys.chord(Keys.SHIFT, "9") + "\""
+ Keys.chord(Keys.SHIFT, "5")
+ "angular <div id='angularTestButton' "
+ "ng" + Keys.chord(Keys.SUBTRACT) + "click='myVar=myVar+1'>"
+ "BindingTest_{{myVar}}_</div>\")");
paragraph1Editor.sendKeys(Keys.chord(Keys.SHIFT, Keys.ENTER));
setTextOfParagraph(1, "println(\"%angular <div id=\\'angularTestButton\\' ng-click=\\'myVar=myVar+1\\'>BindingTest_{{myVar}}_</div>\")");
runParagraph(1);
waitForParagraph(1, "FINISHED");

// check expected text
Expand All @@ -98,9 +93,8 @@ public void testAngularDisplay() throws Exception {
* z.angularBind("myVar", 1)
*/
assertEquals(1, driver.findElements(By.xpath(getParagraphXPath(2) + "//textarea")).size());
WebElement paragraph2Editor = driver.findElement(By.xpath(getParagraphXPath(2) + "//textarea"));
paragraph2Editor.sendKeys("z.angularBind" + Keys.chord(Keys.SHIFT, "9") + "\"myVar\", 1)");
paragraph2Editor.sendKeys(Keys.chord(Keys.SHIFT, Keys.ENTER));
setTextOfParagraph(2, "z.angularBind(\"myVar\", 1)");
runParagraph(2);
waitForParagraph(2, "FINISHED");

// check expected text
Expand All @@ -112,11 +106,8 @@ public void testAngularDisplay() throws Exception {
* print variable
* print("myVar="+z.angular("myVar"))
*/
WebElement paragraph3Editor = driver.findElement(By.xpath(getParagraphXPath(3) + "//textarea"));
paragraph3Editor.sendKeys(
"print" + Keys.chord(Keys.SHIFT, "9") + "\"myVar=\"" + Keys.chord(Keys.ADD)
+ "z.angular" + Keys.chord(Keys.SHIFT, "9") + "\"myVar\"))");
paragraph3Editor.sendKeys(Keys.chord(Keys.SHIFT, Keys.ENTER));
setTextOfParagraph(3, "print(\"myVar=\"+z.angular(\"myVar\"))");
runParagraph(3);
waitForParagraph(3, "FINISHED");

// check expected text
Expand All @@ -139,13 +130,8 @@ public void testAngularDisplay() throws Exception {
* z.run(2, context)
* }
*/
WebElement paragraph4Editor = driver.findElement(By.xpath(getParagraphXPath(4) + "//textarea"));
paragraph4Editor.sendKeys(
"z.angularWatch" + Keys.chord(Keys.SHIFT, "9") + "\"myVar\", "
+ Keys.chord(Keys.SHIFT, "9")
+ "before:Object, after:Object, context:org.apache.zeppelin.interpreter.InterpreterContext)"
+ Keys.EQUALS + ">{ z.run" +Keys.chord(Keys.SHIFT, "9") + "2, context)}");
paragraph4Editor.sendKeys(Keys.chord(Keys.SHIFT, Keys.ENTER));
setTextOfParagraph(4, "z.angularWatch(\"myVar\", (before:Object, after:Object, context:org.apache.zeppelin.interpreter.InterpreterContext)=>{ z.run(2, context)})");
runParagraph(4);
waitForParagraph(4, "FINISHED");


Expand All @@ -168,10 +154,8 @@ public void testAngularDisplay() throws Exception {
* Unbind
* z.angularUnbind("myVar")
*/
WebElement paragraph5Editor = driver.findElement(By.xpath(getParagraphXPath(5) + "//textarea"));
paragraph5Editor.sendKeys(
"z.angularUnbind" + Keys.chord(Keys.SHIFT, "9") + "\"myVar\")");
paragraph5Editor.sendKeys(Keys.chord(Keys.SHIFT, Keys.ENTER));
setTextOfParagraph(5, "z.angularUnbind(\"myVar\")");
runParagraph(5);
waitForParagraph(5, "FINISHED");

// check expected text
Expand All @@ -181,8 +165,7 @@ public void testAngularDisplay() throws Exception {
/*
* Bind again and see rebind works.
*/
paragraph2Editor = driver.findElement(By.xpath(getParagraphXPath(2) + "//textarea"));
paragraph2Editor.sendKeys(Keys.chord(Keys.SHIFT, Keys.ENTER));
runParagraph(2);
waitForParagraph(2, "FINISHED");

// check expected text
Expand Down Expand Up @@ -228,11 +211,8 @@ public void testSparkInterpreterDependencyLoading() throws Exception {
// wait for first paragraph's " READY " status text
waitForParagraph(1, "READY");

WebElement paragraph1Editor = driver.findElement(By.xpath(getParagraphXPath(1) + "//textarea"));

paragraph1Editor.sendKeys("import org.apache.commons.csv.CSVFormat");
paragraph1Editor.sendKeys(Keys.chord(Keys.SHIFT, Keys.ENTER));
waitForParagraph(1, "FINISHED");
setTextOfParagraph(1, "import org.apache.commons.csv.CSVFormat");
runParagraph(1);

// check expected text
assertTrue(waitForText("import org.apache.commons.csv.CSVFormat",
Expand Down

0 comments on commit 0249b49

Please sign in to comment.