1

I want to write an automated JUnit test script with Selenium Web Driver to load a Wikipedia page and then change the title of the page loaded.

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;



public class wikiTest {
  private WebDriver driver;
  private String baseUrl;
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  @Before
  public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "https://www.wikipedia.org/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void testwiki() throws Exception {
      WebDriverWait wait;
    driver.get(baseUrl + "/");
    driver.findElement(By.id("searchInput")).clear();
    driver.findElement(By.id("searchInput")).sendKeys("fish anatomy");
    driver.findElement(By.name("go")).click();
    wait = new WebDriverWait(driver, 10);
    //driver.findElement(By.id("firstHeading")).click();

    /* in here I want to change the title of the page
    */

  }

  @After
  public void tearDown() throws Exception {
   // driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);
    }
  }

  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
        e.printStackTrace();
      return false;
    }
  }

  private boolean isAlertPresent() {
    try {
      driver.switchTo().alert();
      return true;
    } catch (NoAlertPresentException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
}

Any help is appreciated.

1 Answer 1

3

I'm not sure why you would ever want to do that, but one way is to simply execute some javascript.

import org.openqa.selenium.JavascriptExecutor;    

((JavascriptExecutor)driver).executeScript("document.title = 'new title'");

EDIT

Changing text on a webpage (as per the comments) is achievable with pretty much the same method. just execute some javascript. so, inside the quotes instead of document.title = 'new title' use document.getElementById('firstHeading').innerHTML = 'new text' or for modern browsers use textContent instead of innerHTML.

7
  • But this is do nothing. Sep 3, 2015 at 18:42
  • it works for me. do you have any kind of error or anything? Sep 3, 2015 at 19:13
  • No errors was shown.It just load the page and then shows test is 100% passed . I just import the libraries and then enter the other line you mentioned in the position where I mentioned where it need to be execute. Sep 3, 2015 at 19:25
  • Are you asking how to temporarily change the title shown in the tab at the top of the screen? Or are you asking how to edit a Wikipedia article and modify the articles title? I suspect you are really asking for the latter, whilst you appeared to be asking for the former :)
    – Ardesco
    Sep 4, 2015 at 10:16
  • Yes.the solution given by @Xyclos do the former one.but is there any way to do the latter? Sep 7, 2015 at 13:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.