You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RichTextAreaCreateLinkTest.java 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package com.vaadin.tests.components.richtextarea;
  2. import static org.junit.Assert.assertTrue;
  3. import java.time.Duration;
  4. import org.junit.Before;
  5. import org.junit.Test;
  6. import org.openqa.selenium.Alert;
  7. import org.openqa.selenium.By;
  8. import org.openqa.selenium.Keys;
  9. import org.openqa.selenium.NoSuchElementException;
  10. import org.openqa.selenium.WebDriver;
  11. import org.openqa.selenium.WebElement;
  12. import org.openqa.selenium.support.ui.ExpectedConditions;
  13. import org.openqa.selenium.support.ui.FluentWait;
  14. import org.openqa.selenium.support.ui.Wait;
  15. import com.vaadin.testbench.elements.RichTextAreaElement;
  16. import com.vaadin.tests.tb3.MultiBrowserTest;
  17. public class RichTextAreaCreateLinkTest extends MultiBrowserTest {
  18. private static final Duration PROMPT_POLLING_INTERVAL = Duration
  19. .ofSeconds(5);
  20. private static final Duration PROMPT_TIMEOUT_INTERVAL = Duration
  21. .ofSeconds(30);
  22. private static final String TESTING_URI = "https://vaadin.com/";
  23. private static final String TESTING_TEXT = "Vaadin company name";
  24. RichTextAreaElement rta;
  25. @Before
  26. public void init() {
  27. openTestURL();
  28. rta = $(RichTextAreaElement.class).first();
  29. }
  30. @Test
  31. public void createLinkButtonShouldInsertUriAsTextAndHrefIfNoTextIsHighlighted() {
  32. createLinkViaButton(rta, TESTING_URI);
  33. String expected = "<a href=\"" + TESTING_URI + "\">" + TESTING_URI
  34. + "</a>";
  35. assertTrue(String.format(
  36. "RichTextArea's expected value is: %s. However, the following value was received: %s.",
  37. expected, rta.getValue()), rta.getValue().equals(expected));
  38. }
  39. @Test
  40. public void createLinkButtonShouldAddUriAsHrefIfTextIsHighlighted() {
  41. rta.setValue(TESTING_TEXT);
  42. WebElement textArea = rta.findElement(By.className("gwt-RichTextArea"));
  43. textArea.sendKeys(Keys.CONTROL, "a");
  44. createLinkViaButton(rta, TESTING_URI);
  45. String expected = "<a href=\"" + TESTING_URI + "\">" + TESTING_TEXT
  46. + "</a>";
  47. assertTrue(String.format(
  48. "RichTextArea's expected value is: %s. However, the following value was received: %s.",
  49. expected, rta.getValue()), rta.getValue().equals(expected));
  50. }
  51. private void createLinkViaButton(RichTextAreaElement rta, String Uri) {
  52. rta.findElement(By.cssSelector("div[title='Create Link']")).click();
  53. Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
  54. .withTimeout(PROMPT_TIMEOUT_INTERVAL)
  55. .pollingEvery(PROMPT_POLLING_INTERVAL)
  56. .ignoring(NoSuchElementException.class);
  57. // Wait for the alert to be displayed and store it in a variable
  58. Alert alert = wait.until(ExpectedConditions.alertIsPresent());
  59. // Type the URI
  60. alert.sendKeys(Uri);
  61. // Press the OK button
  62. alert.accept();
  63. }
  64. }