Skip to content

Commit

Permalink
add tooltip support
Browse files Browse the repository at this point in the history
  • Loading branch information
grossopa committed Dec 14, 2024
1 parent 80f3304 commit e06706f
Show file tree
Hide file tree
Showing 4 changed files with 248 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,15 @@ public MuiPickersDialog toPickersDialog() {
return new MuiPickersDialog(component, driver, config);
}

/**
* Wraps the current {@link WebComponent} to {@link MuiPickersDialog}.
*
* @return the wrapped {@link MuiPickersDialog} instance on the given component
*/
public MuiTooltip toTooltip() {
return new MuiTooltip(component, driver, config);
}

private <T> T create(Supplier<T> v4creatorFunc, Supplier<T> v5creatorFunc) {
if (config.getVersion() == V4) {
return v4creatorFunc.get();
Expand All @@ -963,4 +972,5 @@ private <T> T create(Supplier<T> v4creatorFunc, Supplier<T> v5creatorFunc) {
public MuiConfig getConfig() {
return config;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright © 2024 the original author or authors.
*
* Licensed under the The MIT License (MIT) (the "License");
* You may obtain a copy of the License at
*
* https://mit-license.org/
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the “Software”), to deal in the Software without
* restriction, including without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.github.grossopa.selenium.component.mui.v4.datadisplay;

import com.github.grossopa.selenium.component.mui.MuiVersion;
import com.github.grossopa.selenium.component.mui.config.MuiConfig;
import com.github.grossopa.selenium.component.mui.v4.AbstractMuiComponent;
import com.github.grossopa.selenium.core.ComponentWebDriver;
import org.openqa.selenium.WebElement;

import java.util.EnumSet;
import java.util.Set;

import static com.github.grossopa.selenium.component.mui.MuiVersion.V4;
import static com.github.grossopa.selenium.component.mui.MuiVersion.V5;

/**
* Tooltips display informative text when users hover over, focus on, or tap an element.
*
* @author Jack Yin
* @since 1.12.0
*/
public class MuiTooltip extends AbstractMuiComponent {

/**
* The component name
*/
public static final String COMPONENT_NAME = "Tooltip";

/**
* Constructs an instance with the delegated element and root driver
*
* @param element the delegated element
* @param driver the root driver
* @param config the Material UI configuration
*/
public MuiTooltip(WebElement element, ComponentWebDriver driver, MuiConfig config) {
super(element, driver, config);
}

@Override
public boolean validate() {
return config.validateComponentByCss(this, "Popper")
&& attributeContains("role", "tooltip");
}

@Override
public Set<MuiVersion> versions() {
return EnumSet.of(V4, V5);
}

@Override
public String getComponentName() {
return COMPONENT_NAME;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright © 2024 the original author or authors.
*
* Licensed under the The MIT License (MIT) (the "License");
* You may obtain a copy of the License at
*
* https://mit-license.org/
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the “Software”), to deal in the Software without
* restriction, including without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.github.grossopa.selenium.component.mui.v4.datadisplay;

import com.github.grossopa.selenium.component.mui.MuiVersion;
import com.github.grossopa.selenium.component.mui.config.MuiConfig;
import com.github.grossopa.selenium.core.ComponentWebDriver;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.WebElement;

import static com.github.grossopa.selenium.component.mui.MuiVersion.V4;
import static com.github.grossopa.selenium.component.mui.MuiVersion.V5;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
* Tests for {@link MuiTooltip}
*
* @author Jack Yin
* @since 1.12.0
*/
class MuiTooltipTest {
MuiTooltip testSubject;
WebElement element = mock(WebElement.class);
ComponentWebDriver driver = mock(ComponentWebDriver.class);
MuiConfig config = mock(MuiConfig.class);

@BeforeEach
void setUp() {
testSubject = new MuiTooltip(element, driver, config);
}

@Test
void versions() {
Assertions.assertArrayEquals(new MuiVersion[]{V4, V5}, testSubject.versions().toArray());
}

@Test
void getComponentName() {
Assertions.assertEquals("Tooltip", testSubject.getComponentName());
}

@Test
void testToString() {
when(element.toString()).thenReturn("element-toString");
Assertions.assertEquals("MuiTooltip{element=element-toString}", testSubject.toString());
}

@Test
void validate() {
when(element.getAttribute("role")).thenReturn("tooltip");
when(config.validateComponentByCss(testSubject, "Popper")).thenReturn(true);
Assertions.assertTrue(testSubject.validate());
}

@Test
void validateNegative() {
when(element.getAttribute("role")).thenReturn("dialog");
when(config.validateComponentByCss(testSubject, "Popper")).thenReturn(true);
Assertions.assertFalse(testSubject.validate());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright © 2021 the original author or authors.
*
* Licensed under the The MIT License (MIT) (the "License");
* You may obtain a copy of the License at
*
* https://mit-license.org/
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the “Software”), to deal in the Software without
* restriction, including without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.github.grossopa.selenium.examples.mui.v5.datadisplay;

import com.github.grossopa.selenium.component.mui.v4.datadisplay.MuiTooltip;
import com.github.grossopa.selenium.component.mui.v4.inputs.MuiButton;
import com.github.grossopa.selenium.core.locator.By2;
import com.github.grossopa.selenium.examples.helper.AbstractBrowserSupport;
import org.openqa.selenium.By;

import java.util.List;

import static com.github.grossopa.selenium.component.mui.MuiComponents.muiV5;
import static com.github.grossopa.selenium.core.driver.WebDriverType.EDGE;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Test cases for {@link MuiTooltip}.
*
* @author Jack Yin
* @since 1.12.0
*/
public class MuiTooltipTestCases extends AbstractBrowserSupport {

/**
* Tests the basic lists.
*
* @see <a href="https://mui.com/material-ui/react-divider/#basic-list">
* https://mui.com/material-ui/react-divider/#basic-list</a>
*/
public void testBasicList() {
List<MuiButton> buttons = driver.findComponent(By.id("BasicTooltip.js")).findComponent(By2.parent())
.findComponentsAs(By2.attrExact("aria-label", "Delete"), c -> c.as(muiV5()).toButton());
assertEquals(1, buttons.size());

driver.moveTo(buttons.get(0));
driver.threadSleep(1000L);

MuiTooltip tooltip = driver.findComponent(By2.attrExact("role", "tooltip")).as(muiV5()).toTooltip();

assertTrue(tooltip.validate());
assertEquals("Delete", tooltip.getText());
}

public static void main(String[] args) {
MuiTooltipTestCases test = new MuiTooltipTestCases();
test.setUpDriver(EDGE);
test.driver.navigate().to("https://mui.com/material-ui/react-tooltip/");

test.testBasicList();
}
}

0 comments on commit e06706f

Please sign in to comment.