Ana içeriğe atla

Selenium Kod Denemeleri


Google'ın Title'ını Kontrol

import static org.junit.Assert.*;

import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebDriver;
//import static org.junit.Assert.assertEquals;

public class VerifyTitle_Google {
    private WebDriver driver;

    @Before
    public void setUp() throws Exception {
        System.setProperty("webdriver.gecko.driver","C:\\Selenium\\geckodriver-v0.23.0-win64\\geckodriver.exe");//mozillanin herseyine gecko yazican
        driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
    }

    @After
    public void tearDown() throws Exception {
        driver.quit();//sayfayi kapatiyor hemen testten sonra
    }

    @Test
    public void test() {
               
        driver.get("https://www.google.com/");
       
        String ActualTitle = driver.getTitle();
        String ExpectedTitle = "Google";//title ı f12 ye basarak html kodundan kontrol et, ezbere yazma
        assertEquals(ActualTitle, ExpectedTitle);
    }

}

Google'ın Title'ını ve Google'da Arama Sonucu Çıkan Sayfanın Title'ını Kontrol

Burda iki test aynı koda eklendi, diğerlerinden farklı olarak. Bir kodun içinde iki test çalıştı.

package CommonlyUsedMethods;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import static org.junit.Assert.assertEquals;
import java.util.concurrent.TimeUnit;

public class BasicTestTemplate {
private WebDriver driver;
private String baseUrl;
  @Before
  public void setUp() throws Exception {
  System.setProperty("webdriver.gecko.driver","C:\\Selenium\\geckodriver-v0.23.0-win64\\geckodriver.exe");
  driver = new FirefoxDriver();
    baseUrl = "https://www.google.com";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }
  @After
  public void tearDown() throws Exception {
    driver.quit();
    }
  @Test
  public void test_01() throws Exception {
    driver.get(baseUrl);
    String ExpectedTitle =driver.getTitle();
        assertEquals(ExpectedTitle,"Google");
  }
  @Test
  public void test_02() throws Exception {
driver.get(baseUrl);//google.com ı alıyor
driver.findElement(By.name("q")).sendKeys("Selenium Webdriver");//arama çubuğuna yazdırıyor
driver.findElement(By.xpath("/html/body/div/div[3]/form/div[2]/div/div[3]/center/input[1]")).click();//Google'da Ara ya tıklıyor
    driver.findElement(By.className("LC20lb")).click();//Selenium Webdriver sonucuna tklıyor
    assertEquals(driver.getTitle(),"Selenium WebDriver");// Açılan sayfanın title ı ile Selenium Webdriver ı karşılaştırıyor
}
  }

  LinkText, PartialLinkText


@Test
public void test() throws InterruptedException {
//fail("Not yet implemented");
driver.get("http://localhost/whizTrial/");
driver.findElement(By.linkText("Create New Account")).click();// html kodunda href den sonra gelen yazı kısmı buraya yazılır 
}


@Test
public void test2() throws InterruptedException {
//fail("Not yet implemented");
driver.get("http://localhost/whizTrial/");
driver.findElement(By.partialLinkText("Create New")).click();// html kodunda href den sonra gelen yazının bir kısmını buraya yazılır 
Thread.sleep(2000);
}

  • By.partialLinkText()" yöntemi aşağıdaki iki elemente de ulaşabilir.


HTML:

* <a href ="..." > Blok seviyesi içindeki etiket </a>

* <a href ="..." >
   <div>
         <span> Blok seviyesi dışındaki etiket</span>
    </div>
</a>


ClassName Yazdırma

@Test
public void test1() throws InterruptedException {
//fail("Not yet implemented");
driver.get("http://localhost/whizTrial/");
String name = driver.findElement(By.className("about-author")).getText(); 
System.out.print(name);
}

Yorumlar

Bu blogdaki popüler yayınlar

Selenium - XPath Kodları

XPath, XML Path Absolute XPath: root node u verir ve / ile başlar Elementin path inde herhangi bir değişiklik yapılırsa, test kodu hata verir. Örn:  html/body/div[1]/section/div[1]/div/div/div/div[1]/div/div/div/div/div[3]/div[1]/div/h4[1]/b Relative XPath: xml dokümanındaki elementi verir ve // ile başlar Örn : //*[@class='featured-box']//*[text()='Testing'] XPath Kombinasyonları: Belli bir tagin belli bir attribute değeri ile kullanımı Herhangi bir tag in belli bir attribute değeri ile kullanımı "and" operatörü "or" operatörü Belirli bir değer ile başlayan attribute Metnin bir kısmını içeren attribute Sadece text() Herhangi bir attribute un değeri Belli Bir Tag'ın Belli Bir Attribute Değeri ile Kullanımı (Specific tag with specific attribute value) Html Kodu < input   id = "Lid"  .....   > input:tag adı (etiket adı) id : attribute Lid : attribute değeri Selenium Kodu driver.findElement(By.x...

Otomasyon Testlerini JUnit ile Önceliklendirme

Kullandığınız IDE'de ilk olarak çalışmasını istediğiniz kodun @Test annotation'ının üstüne  @Category({Critical.class}) yazın. İkinci olarak çalışmasını istediğinizin üzerine; @Category({Major.class}) yazın. Bu categoriler için Critical ve Major interfacelerini oluşturun. Critical interface inin içine import org.junit.experimental.categories.Categories; import org.junit.runner.RunWith; @RunWith(Categories.class) @Categories.IncludeCategory({Critical.class}) public interface Critical { } Major categorisinin içine import org.junit.experimental.categories.Categories; import org.junit.runner.RunWith; @RunWith(Categories.class) @Categories.IncludeCategory({Major.class}) public interface Major { } yazın. pom.xml'e <profiles>             <profile>                 <id> All </id>                 <build> ...