Skip to content

Commit

Permalink
#593: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bhecquet committed Mar 15, 2024
1 parent 861dec6 commit 4e76caa
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ public abstract class Element {

protected String label;


// origin is the class where object is declared
// origin may be null if object is created outside of any PageObject (which should never happen)
// origin may be null if object is created outside of any PageObject (which should never happen outside of unit tests)
protected String origin;

// the page instance that uses the object.
Expand Down Expand Up @@ -132,6 +133,10 @@ public PageObject getCallingPage() {
public String getOrigin() {
return origin;
}

public void setOrigin(String origin) {
this.origin = origin;
}
public Class<? extends PageObject> getOriginClass() {
try {
return (Class<? extends PageObject>) Class.forName(origin);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public String getName() {
}

private String getElementLocator(WebElement el) {
// handle: Decorated {<button id="button2" name="resetButton" onclick="javascript:addText('text2', '');javascript:resetState();">} ??

if (el.toString().contains("->")) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.seleniumtests.it.core;
package com.seleniumtests.it.core.aspects;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;

import com.seleniumtests.core.runner.SeleniumRobotTestPlan;
Expand All @@ -42,7 +40,7 @@
import com.seleniumtests.it.driver.support.pages.DriverTestPage;
import com.seleniumtests.reporter.logger.TestStep;

public class TestLogActions extends GenericDriverTest {
public class TestLogActions2 extends GenericDriverTest {


private DriverTestPage testPage;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.seleniumtests.it.core;
package com.seleniumtests.it.core.aspects;

import com.seleniumtests.GenericDriverTest;
import com.seleniumtests.GenericTest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import java.util.List;
import java.util.regex.Pattern;

import com.seleniumtests.it.driver.support.pages.DriverTestPage;
import com.seleniumtests.uipage.PageObject;
import io.appium.java_client.android.options.UiAutomator2Options;
import com.seleniumtests.driver.DriverMode;
import org.mockito.Mock;
Expand Down Expand Up @@ -791,6 +793,10 @@ public void testGetNameWithLabel() {
el.setFieldName("el");
Assert.assertEquals(el.getName(), "myElement");
}

/**
* When label is empty, field name is used, if it exists
*/
@Test(groups = { "ut" })
public void testGetNameWithEmptyLabel() {
HtmlElement el = new HtmlElement("", By.id("foo"));
Expand All @@ -808,5 +814,36 @@ public void testGetNameNoFieldName() {
HtmlElement el = new HtmlElement(null, By.id("foo"));
Assert.assertEquals(el.getName(), "By.id: foo");
}

/**
* Test that when origin is set (the element is created from a PageObject class), origin is returned
*/
@Test(groups = { "ut" })
public void testGetOriginClassWithPage() {
HtmlElement el = spy(new HtmlElement(null, By.id("foo")));
el.setOrigin("com.seleniumtests.it.driver.support.pages.DriverTestPage");
Assert.assertEquals(el.getOriginClass(), DriverTestPage.class);
}
@Test(groups = { "ut" })
public void testGetOriginClassWithInvalidPage() {
HtmlElement el = spy(new HtmlElement(null, By.id("foo")));
when(el.getOrigin()).thenReturn("pages.DriverTestPage");
Assert.assertNull(el.getOriginClass());
}

/**
* When element is not created inside a PageObject, it's origin is "null"
*/
@Test(groups = { "ut" })
public void testGetOriginClassWithNullPage() {
HtmlElement el = new HtmlElement(null, By.id("foo"));
Assert.assertNull(el.getOriginClass());
}
@Test(groups = { "ut" })
public void testGetOriginClassCallingPage() {
HtmlElement el = new HtmlElement(null, By.id("foo"));
el.setCallingPage(new PageObject(null));
Assert.assertEquals(el.getOriginClass(), PageObject.class);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.seleniumtests.ut.uipage.htmlelements;

import static org.mockito.Mockito.when;

import com.seleniumtests.MockitoTest;
import com.seleniumtests.uipage.htmlelements.SeleniumElement;
import org.mockito.Mock;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebElement;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;
import org.testng.annotations.Test;

public class TestSeleniumElement extends MockitoTest {

@Mock
private RemoteWebElement element;



@Test(groups="ut")
public void testGetNameWebElement() {
when(element.toString()).thenReturn("Decorated {[[ChromeDriver: chrome on windows (83c6e18cbc09be7060ef3f92feda6877)] -> id: button2]}");
Assert.assertEquals(new SeleniumElement(element).getName(), "id: button2");
}

@Test(groups="ut")
public void testGetNameSelect() {
when(element.getTagName()).thenReturn("select");
when(element.toString()).thenReturn("Decorated {[[ChromeDriver: chrome on windows (83c6e18cbc09be7060ef3f92feda6877)] -> id: button2]}");
Assert.assertEquals(new SeleniumElement(new Select(element)).getName(), "Select id: button2");
}

@Test(groups="ut")
public void testGetNameHtmlUnitElement() {
when(element.toString()).thenReturn("Decorated {<button id=\"button2\" name=\"resetButton\" onclick=\"javascript:addText('text2', '');javascript:resetState();\">}");
Assert.assertEquals(new SeleniumElement(element).getName(), "Decorated {<button id=\"button2\" name=\"resetButton\" onclick=\"javascript:addText('text2', '');javascript:resetState();\">}");
}

@Test(groups="ut")
public void testGetNameWebElementNull() {
Assert.assertNull(new SeleniumElement((WebElement) null).getName());
}

}

0 comments on commit 4e76caa

Please sign in to comment.