Skip to content

Commit

Permalink
#2 Add MarkdownEditor (as Page Object)
Browse files Browse the repository at this point in the history
- Add MarkdownEditor
-> Move edit functionality from MarkdownViewer to MarkdownEditor
  • Loading branch information
helkv committed Jan 22, 2020
1 parent 98f06f8 commit 603860b
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 52 deletions.
82 changes: 82 additions & 0 deletions src/main/java/ui/pages/MarkdownEditor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package ui.pages;

import static io.cucumber.spring.CucumberTestContext.SCOPE_CUCUMBER_GLUE;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
* Page Object encapsulates the Markdown editor page.
*
* @author helk
*
*/
@Lazy
@Component
@Scope(SCOPE_CUCUMBER_GLUE)
public class MarkdownEditor extends BasePage {

@Autowired
public MarkdownEditor(WebDriver driver) {
super(driver);
}

public MarkdownEditor editContent(String title, String author, String description, String year, String institute) {
this.addTitle(title);
this.addAuthor(author);
// Publisher (has default value)
this.addDescription(description);
this.addYear(year);
this.addInstitute(institute);
// Resource Type (has default value)
// License (Optional)

WebElement saveButton = driver.findElement(By.id("saveButton"));
saveButton.click();

driver.navigate().refresh();
wait.until(ExpectedConditions.stalenessOf(saveButton));
wait.until(ExpectedConditions.elementToBeClickable(By.id("editButton")));

return this;
}

public void addTitle(String title) {
this.addTextToOutline("Title", title);
}

public void addAuthor(String author) {
this.addTextToOutline("Author", author);
}

public void addDescription(String description) {
this.addTextToOutline("Description", description);
}

public void addYear(String year) {
this.addTextToOutline("Year", year);
}

public void addInstitute(String institute) {
this.addTextToOutline("Institute", institute);
}

private void addTextToOutline(String outline, String outlineText) {
WebElement outlineElement = driver
.findElement(By.xpath("//div[@class='outline-h2' and text()='" + outline + "']"));
outlineElement.click();
WebElement outlineInput = driver.switchTo().activeElement();
outlineInput.sendKeys(Keys.END);
outlineInput.sendKeys(Keys.RETURN);
WebElement beneathOutlineInput = driver.switchTo().activeElement();
beneathOutlineInput.sendKeys(outlineText);
}

}
57 changes: 6 additions & 51 deletions src/main/java/ui/pages/MarkdownViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
Expand Down Expand Up @@ -48,6 +47,10 @@ public class MarkdownViewer extends BasePage {
@FindBy(className = "seafile-viewer-outline")
private WebElement outline;

@Lazy
@Autowired
MarkdownEditor markdownEditor;

@Autowired
public MarkdownViewer(WebDriver driver) {
super(driver);
Expand Down Expand Up @@ -95,59 +98,11 @@ public List<String> getOutlineContent() {
return content;
}

public void editFile(String title, String author, String description, String year, String institute) {
public MarkdownEditor edit() {
this.editButton.click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("seafile-editor")));

// TODO: Extract to a new Page Class 'MarkdownEditorPage'
this.addTitle(title);
this.addAuthor(author);
this.addDescription(description);
this.addYear(year);
this.addInstitute(institute);
// TODO: Should: Publisher, Resource Type and License be edited?

WebElement saveButton = driver.findElement(By.id("saveButton"));
saveButton.click();

driver.navigate().refresh();
wait.until(ExpectedConditions.stalenessOf(saveButton));
wait.until(ExpectedConditions.elementToBeClickable(By.id("editButton")));
}

public void addTitle(String title) {
this.addTextToOutline("Title", title);
}

public void addAuthor(String author) {
this.addTextToOutline("Author", author);
}

public void addDescription(String description) {
this.addTextToOutline("Description", description);
}

public void addYear(String year) {
this.addTextToOutline("Year", year);
}

public void addInstitute(String institute) {
this.addTextToOutline("Institute", institute);
}

public void addDOI(String doi) {
this.addTextToOutline("DOI", doi);
}

private void addTextToOutline(String outline, String outlineText) {
WebElement outlineElement = driver
.findElement(By.xpath("//div[@class='outline-h2' and text()='" + outline + "']"));
outlineElement.click();
WebElement outlineInput = driver.switchTo().activeElement();
outlineInput.sendKeys(Keys.END);
outlineInput.sendKeys(Keys.RETURN);
WebElement beneathOutlineInput = driver.switchTo().activeElement();
beneathOutlineInput.sendKeys(outlineText);
return markdownEditor;
}

}
4 changes: 3 additions & 1 deletion src/test/java/stepdefinitions/FileSteps.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import ui.pages.FileViewer;
import ui.pages.MarkdownEditor;
import ui.pages.MarkdownViewer;

/**
Expand Down Expand Up @@ -78,7 +79,8 @@ public void editArchiveMetadata(DataTable archiveMetadataTable) {

Map<String, String> archiveMetadataMap = archiveMetadataTable.asMap(String.class, String.class);

markdownViewer.editFile(archiveMetadataMap.get("title"), archiveMetadataMap.get("author"),
MarkdownEditor markdownEditor = markdownViewer.edit();
markdownEditor.editContent(archiveMetadataMap.get("title"), archiveMetadataMap.get("author"),
archiveMetadataMap.get("description"), archiveMetadataMap.get("year"),
archiveMetadataMap.get("institute"));
}
Expand Down

0 comments on commit 603860b

Please sign in to comment.