Selenium - Maitiro Ekudzoreredza maKuki muNew Browser Window

Ngatitii tinofanirwa kuyedza mamiriro anotevera:

1. Enda kune peji rekupinda uye pinda kuchishandiswa
2. Vhara bhurawuza
3. Vhura bhurawuza uye enda kune iyo peji rekupinda - mushandisi haafanire kuona fomu yekupinda uye anofanira kunge atove akatopinda mukati.

Pakutanga kupinda, makuki anochengetwa mubrowser. MuWebDriver, kana hwindo rebrowser rakavharwa, data rese remakesi uye makuki anobviswa, saka kuyedzwa kwenzvimbo iri pamusoro hakuite.

Neraki, WebDriver ine mashandiro ekuverenga makuki kubva mubrowser usati waivhara uye wozodzorera makuki mune new browser hwindo.

import org.openqa.selenium.By; import org.openqa.selenium.Cookie; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.Assert; import java.util.Set; public class CookieTest {
WebDriver driver;
@Test
public void login_state_should_be_restored() {
driver = new FirefoxDriver();

driver.get('http://www.example.com/login');
driver.findElement(By.id('username')).sendKeys('admin');
driver.findElement(By.id('password')).sendKeys('12345');
driver.findElement(By.id('login')).click();

Assert.assertTrue(


driver.findElement(By.id('welcome')).isDisplayed());

//Before closing the browser, read the cookies
Set allCookies = driver.manage().getCookies();

driver.close();

//open a new browser window
driver = new FirefoxDriver();

//restore all cookies from previous session
for(Cookie cookie : allCookies) {

driver.manage().addCookie(cookie);
}

driver.get('http://www.example.com/login'); //Login page should not be disaplyed
Assert.assertTrue(


driver.findElement(By.id('welcome')).isDisplayed());

driver.close();
} }