Created
April 14, 2021 19:23
-
-
Save Jaimin180296/897274dce8ee474d123a2f89cddfde00 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io.appium.java_client.ios.IOSDriver; | |
import org.openqa.selenium.By; | |
import org.openqa.selenium.JavascriptExecutor; | |
import org.openqa.selenium.WebElement; | |
import org.openqa.selenium.remote.DesiredCapabilities; | |
import org.openqa.selenium.support.ui.ExpectedConditions; | |
import org.openqa.selenium.support.ui.WebDriverWait; | |
import org.testng.annotations.AfterTest; | |
import org.testng.annotations.BeforeClass; | |
import org.testng.annotations.Test; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.util.*; | |
public class IosPopOpenNewWindow { | |
public IOSDriver driver; | |
public WebDriverWait wait; | |
public static String AUTOMATE_USERNAME = "<username>"; | |
public static String AUTOMATE_ACCESS_KEY = "<accesskey>"; | |
public static final String URL = "https://" + AUTOMATE_USERNAME + ":" + AUTOMATE_ACCESS_KEY + "@hub-cloud.browserstack.com/wd/hub"; | |
static String currentwindow; | |
@BeforeClass | |
public void setup() throws MalformedURLException { | |
DesiredCapabilities caps = new DesiredCapabilities(); | |
caps.setCapability("os_version", "12"); | |
caps.setCapability("device", "iPhone XR"); | |
caps.setCapability("real_mobile", "true"); | |
caps.setCapability("browserstack.appium_version", "1.18.0"); | |
caps.setCapability("browserstack.local", "false"); | |
caps.setCapability("safariAllowPopups", "true"); | |
driver = new IOSDriver(new URL(URL), caps); | |
} | |
@Test | |
public void display() throws InterruptedException { | |
wait = new WebDriverWait(driver, 30); | |
driver.get("https://demoqa.com/browser-windows"); | |
JavascriptExecutor js = driver; | |
currentwindow = driver.getWindowHandle(); | |
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@id='tabButton']"))); | |
js.executeScript("arguments[0].scrollIntoView();", element); | |
element.click(); | |
driver.switchTo().alert().accept(); | |
String mainWindowHandle = driver.getWindowHandle(); | |
Set<String> allWindowHandles = driver.getWindowHandles(); | |
Iterator<String> iterator = allWindowHandles.iterator(); | |
while (iterator.hasNext()) { | |
String ChildWindow = iterator.next(); | |
if (!mainWindowHandle.equalsIgnoreCase(ChildWindow)) { | |
driver.switchTo().window(ChildWindow); | |
Thread.sleep(5000); | |
driver.close(); | |
} | |
} | |
driver.switchTo().window(mainWindowHandle); | |
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@id='tabButton']"))); | |
} | |
@AfterTest | |
public void destroy() { | |
driver.quit(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment