Skip to content

Instantly share code, notes, and snippets.

@Jaimin180296
Created June 18, 2021 20:48
Show Gist options
  • Save Jaimin180296/598c10589e2686f4de5704d2a80f60f1 to your computer and use it in GitHub Desktop.
Save Jaimin180296/598c10589e2686f4de5704d2a80f60f1 to your computer and use it in GitHub Desktop.
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.Iterator;
import java.util.Set;
public class IosTabs {
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", "13");
caps.setCapability("device", "iPhone 11 pro");
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://skillsforall.com/");
JavascriptExecutor js = driver;
currentwindow = driver.getWindowHandle();
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@href='https://skillsforall.com/p/privacy.html']")));
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);
}
@AfterTest
public void destroy() {
driver.quit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment