Skip to content

Commit

Permalink
add more samples of Selenide features
Browse files Browse the repository at this point in the history
  • Loading branch information
asolntsev committed Sep 8, 2024
1 parent f1ac418 commit ef53eaa
Show file tree
Hide file tree
Showing 9 changed files with 214 additions and 15 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'java'
defaultTasks 'clean', 'test'

ext {
selenideVersion = '7.4.2'
selenideVersion = '7.4.3'
junitVersion = '5.11.0'
assertjVersion = '3.26.3'
slf4jVersion = '2.0.16'
Expand Down
46 changes: 46 additions & 0 deletions src/test/java/org/selenide/bs/AnnotatedPageObjectTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.selenide.bs;

import com.codeborne.selenide.ElementsCollection;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.support.FindBy;

import static com.codeborne.selenide.CollectionCondition.sizeGreaterThan;
import static com.codeborne.selenide.CollectionCondition.sizeLessThan;
import static com.codeborne.selenide.Selenide.open;
import static com.codeborne.selenide.Selenide.page;

public class AnnotatedPageObjectTest {
@BeforeEach
void setUp() {
open("https://selenide.org/users.html");
}

@Test
void showsAllKnownSelenideUsers() {
SelenideUsersPage page = page();
page.users.shouldHave(sizeGreaterThan(40));
}

@Test
void canFilterByTag() {
SelenideUsersPage page = page();
page.filterByTag("usa");
page.users.shouldHave(sizeLessThan(40));
}

private static class SelenideUsersPage {
@FindBy(css = "#selenide-users .user:not(.hidden)")
ElementsCollection users;

@FindBy(css = "#user-tags .tag")
ElementsCollection userTags;

public void filterByTag(String tag) {
userTags.stream()
.filter(el -> el.getText().equals(tag))
.findAny().orElseThrow(() -> new AssertionError("User tag not found: " + tag))
.click();
}
}
}
42 changes: 42 additions & 0 deletions src/test/java/org/selenide/bs/DropdownTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.selenide.bs;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static com.codeborne.selenide.Condition.text;
import static com.codeborne.selenide.Condition.value;
import static com.codeborne.selenide.Selenide.$;
import static com.codeborne.selenide.Selenide.open;

public final class DropdownTest {
@BeforeEach
void setUp() {
open("https://selenide.org/test-page/dropdown.html");
}

@Test
void canSelectOptionByText() {
$("#hero").selectOption("Sarah Connor");
$("#hero").getSelectedOption().shouldHave(value("sarah-connor"));
$("#hero-summary").shouldHave(text("Your hero is: Sarah Connor (id: sarah-connor)"));
}

@Test
void canSelectOptionBySubstring() {
$("#hero").selectOptionContainingText("hn Mc");
$("#hero").getSelectedOption().shouldHave(text("John McClane"));
$("#hero-summary").shouldHave(text("Your hero is: John McClane (id: john-mcclane)"));
}

@Test
void canSelectOptionByIndex() {
$("#hero").selectOption(3);
$("#hero-summary").shouldHave(text("Your hero is: Ellen Ripley (id: ellen-ripley)"));
}

@Test
void canSelectOptionByValue() {
$("#hero").selectOptionByValue("samwise-gamgee");
$("#hero-summary").shouldHave(text("Your hero is: Samwise Gamgee (id: samwise-gamgee)"));
}
}
6 changes: 6 additions & 0 deletions src/test/java/org/selenide/bs/FileDownloadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ void downloadFile_using_httpGet() {
checkDownload(HTTPGET);
}

@Test
@Disabled
void downloadFile_using_cdp() {
checkDownload(CDP);
}

@Test
@Disabled
void downloadFile_using_folder() {
Expand Down
28 changes: 14 additions & 14 deletions src/test/java/org/selenide/bs/FileUploadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@
import static com.codeborne.selenide.Selenide.open;

class FileUploadTest {
@Test
void canUploadFile() throws IOException {
File file = createTemporaryFile();
@Test
void canUploadFile() throws IOException {
File file = createTemporaryFile();

open("https://the-internet.herokuapp.com/upload");
$("#file-upload").uploadFile(file);
$("#file-submit").click();
$("#uploaded-files").shouldHave(text(file.getName()));
}
open("https://the-internet.herokuapp.com/upload");
$("#file-upload").uploadFile(file);
$("#file-submit").click();
$("#uploaded-files").shouldHave(text(file.getName()));
}

private File createTemporaryFile() throws IOException {
File tempFile = File.createTempFile("selenide-browserstack.", ".temp.txt");
try (Writer w = new FileWriter(tempFile)) {
w.write("Hello, world!");
}
return tempFile;
private File createTemporaryFile() throws IOException {
File tempFile = File.createTempFile("selenide-browserstack.", ".temp.txt");
try (Writer w = new FileWriter(tempFile)) {
w.write("Hello, world!");
}
return tempFile;
}
}
20 changes: 20 additions & 0 deletions src/test/java/org/selenide/bs/LocalStorageTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.selenide.bs;

import org.junit.jupiter.api.Test;

import static com.codeborne.selenide.LocalStorageConditions.item;
import static com.codeborne.selenide.LocalStorageConditions.itemWithValue;
import static com.codeborne.selenide.Selenide.localStorage;
import static com.codeborne.selenide.Selenide.open;

public class LocalStorageTest {
@Test
void canCheckLocalStorage() {
open("https://selenide.org/test-page");
localStorage().shouldNotHave(item("event"));

localStorage().setItem("event", "Testμ 2024");
localStorage().shouldHave(item("event"));
localStorage().shouldHave(itemWithValue("event", "Testμ 2024"));
}
}
20 changes: 20 additions & 0 deletions src/test/java/org/selenide/bs/SessionStorageTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.selenide.bs;

import org.junit.jupiter.api.Test;

import static com.codeborne.selenide.Selenide.open;
import static com.codeborne.selenide.Selenide.sessionStorage;
import static com.codeborne.selenide.SessionStorageConditions.item;
import static com.codeborne.selenide.SessionStorageConditions.itemWithValue;

public class SessionStorageTest {
@Test
void canCheckSessionStorage() {
open("https://selenide.org/test-page");
sessionStorage().shouldNotHave(item("event"));

sessionStorage().setItem("event", "Testμ 2024");
sessionStorage().shouldHave(item("event"));
sessionStorage().shouldHave(itemWithValue("event", "Testμ 2024"));
}
}
40 changes: 40 additions & 0 deletions src/test/java/org/selenide/bs/SimplePageObjectTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.selenide.bs;

import com.codeborne.selenide.ElementsCollection;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static com.codeborne.selenide.CollectionCondition.sizeGreaterThan;
import static com.codeborne.selenide.CollectionCondition.sizeLessThan;
import static com.codeborne.selenide.Selectors.byText;
import static com.codeborne.selenide.Selenide.*;

public class SimplePageObjectTest {
@BeforeEach
void setUp() {
open("https://selenide.org/users.html");
}

@Test
void showsAllKnownSelenideUsers() {
SelenideUsersPage page = new SelenideUsersPage();
page.users().shouldHave(sizeGreaterThan(40));
}

@Test
void canFilterByTag() {
SelenideUsersPage page = new SelenideUsersPage();
page.filterByTag("usa");
page.users().shouldHave(sizeLessThan(40));
}

private static class SelenideUsersPage {
public ElementsCollection users() {
return $$("#selenide-users .user:not(.hidden)");
}

public void filterByTag(String tag) {
$("#user-tags").find(byText(tag)).click();
}
}
}
25 changes: 25 additions & 0 deletions src/test/java/org/selenide/bs/WebdriverConditionsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.selenide.bs;

import org.junit.jupiter.api.Test;

import static com.codeborne.selenide.Selectors.byText;
import static com.codeborne.selenide.Selenide.*;
import static com.codeborne.selenide.WebDriverConditions.title;
import static com.codeborne.selenide.WebDriverConditions.*;

public class WebdriverConditionsTest {
@Test
void search() {
open("https://selenide.org");
$(".main-menu-pages").find(byText("Users")).click();

webdriver().shouldHave(url("https://selenide.org/users.html"));
webdriver().shouldHave(urlStartingWith("https://selenide.org"));
webdriver().shouldHave(urlContaining("selenide.org/users"));
webdriver().shouldHave(currentFrameUrl("https://selenide.org/users.html"));
webdriver().shouldHave(numberOfWindows(1));
webdriver().shouldHave(title("Selenide users"));
webdriver().shouldNotHave(cookie("session", "sid-12345"));
webdriver().shouldNotHave(cookie("session"));
}
}

0 comments on commit ef53eaa

Please sign in to comment.