Project

General

Profile

« Previous | Next » 

Revision bcc70074

Added by Andreas Kohlbecker over 3 years ago

fix #9186 test for speciment detrivate path view & DescriptionList web element wrapper

View differences:

modules/cdm_dataportal/cdm_dataportal.module
2838 2838
  $dynabox_id = preg_replace('/[^a-zA-Z0-9\-]/', '', $dynabox_id);
2839 2839

  
2840 2840
  if(!array_key_exists('class', $attributes)) {
2841
    $attributes['class'] = array();
2841
    $attributes['class'] = ["dynabox"];
2842
  } else {
2843
    $attributes['class'] = array_merge($attributes['class'], ["dynabox"]);
2842 2844
  }
2843 2845
  $attributes['id'][] = 'dynabox-' . $dynabox_id;
2844 2846
  $dynabox_attributes = drupal_attributes($attributes);
src/main/java/eu/etaxonomy/dataportal/ElementUtils.java
11 11
import java.util.ArrayList;
12 12
import java.util.List;
13 13

  
14
import org.apache.commons.lang3.StringUtils;
14 15
import org.apache.log4j.Logger;
15 16
import org.openqa.selenium.By;
16 17
import org.openqa.selenium.WebElement;
......
117 118
        return ElementUtils.baseElementsFromFootNoteListElements(fnListElements);
118 119
    }
119 120

  
121
    /**
122
     * Intended for loggin purposes.
123
     * <p>
124
     * Creates incomplete markup of the WebElement with the attributes id and class like:
125
     * {@code <tagName id="the-id" class="">
126
     *
127
     * @param we the WebElement
128
     * @return the markup
129
     */
130
    public static String webElementTagToMarkup(WebElement we) {
131
        String markup = "<" + we.getTagName();
132
        String idAttr = we.getAttribute("id");
133
        String classAttr = we.getAttribute("class");
134
        if(StringUtils.isNotEmpty(idAttr)) {
135
            markup += " id=\""+ classAttr + "\"";
136
        }
137
        if(StringUtils.isNotEmpty(classAttr)) {
138
            markup += " class=\""+ classAttr + "\"";
139
        }
140
        markup += " >";
141
        return markup;
142
    }
143

  
120 144

  
121 145
}
src/main/java/eu/etaxonomy/dataportal/elements/DescriptionList.java
1
/**
2
* Copyright (C) 2020 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.dataportal.elements;
10

  
11
import java.util.ArrayList;
12
import java.util.HashMap;
13
import java.util.List;
14
import java.util.Map;
15
import java.util.stream.Collectors;
16

  
17
import org.openqa.selenium.By;
18
import org.openqa.selenium.NoSuchElementException;
19
import org.openqa.selenium.WebElement;
20

  
21
import com.thoughtworks.selenium.SeleniumException;
22

  
23
import eu.etaxonomy.dataportal.ElementUtils;
24

  
25
/**
26
 * @author a.kohlbecker
27
 * @since Aug 13, 2020
28
 */
29
public class DescriptionList extends BaseElement {
30

  
31
    private Map<String, List<DescriptionElement>> descriptionGroups = new HashMap<>();
32

  
33
    /**
34
     * @param element A WebElement for a DescriptionList ('{@code<dl>}') DOM object.
35
     */
36
    public DescriptionList(WebElement element) {
37
        super(element);
38
        if(!element.getTagName().equals("dl")) {
39
            throw new SeleniumException("Expecting DescriptionList (<dl>) as paramter element, but was " + ElementUtils.webElementTagToMarkup(element));
40
        }
41
        List<DescriptionElement> descriptionElements = null;
42
        for(WebElement we : element.findElements(By.xpath("./child::*"))){
43
            if(we.getTagName().equals("dt")) {
44
                descriptionElements = new ArrayList<>();
45
                descriptionGroups.put(we.getText(), descriptionElements);
46
            }
47
            if(we.getTagName().equals("dd")) {
48
                if(descriptionElements == null) {
49
                    // oops, this is a dd element without preceding dt
50
                    descriptionElements = new ArrayList<>();
51
                    descriptionGroups.put("MISSING DT ELEMENT BEFORE DD", descriptionElements);
52
                }
53
                descriptionElements.add(new DescriptionElement(we));
54
            }
55
        }
56
    }
57

  
58
    /**
59
     *
60
     * @return the descriptionGroups
61
     */
62
    public Map<String, List<DescriptionElement>> getDescriptionGroups() {
63
        return descriptionGroups;
64
    }
65

  
66
    /**
67
     *
68
     * @param descriptionGroupsKey The text of the {@code <dt>} element preceding
69
     * one or more {@code <dd>} elements. Text of multiple {@code <dd>} will be joined
70
     * using the new line ('{@code \n}') character.
71
     *
72
     * @return the DescriptionElementTexts joined with '{@code \n}' or null
73
     * if the key is not present in the map.
74
     */
75
    public String joinedDescriptionElementText(String descriptionGroupsKey) {
76
        String joinedText = null;
77
        if(descriptionGroups.containsKey((descriptionGroupsKey))) {
78
            joinedText = descriptionGroups.get(descriptionGroupsKey)
79
                    .stream()
80
                    .map(de -> de.getDescriptionElementText())
81
            .collect(Collectors.joining("\n"));
82
        }
83
        return joinedText;
84
    }
85

  
86
    public static class DescriptionElement {
87

  
88
        private DescriptionList subList = null;
89
        private BaseElement descriptionElementContent = null;
90
        private String descriptionElementText = null;
91

  
92
        DescriptionElement(WebElement descriptionElement){
93
            try {
94
                subList = new DescriptionList(descriptionElement.findElement(By.tagName("dl")));
95
                descriptionElementContent = subList;
96
            } catch (NoSuchElementException e1) {
97
                try {
98
                    descriptionElementContent = new BaseElement(descriptionElement.findElement(By.xpath("./child::*")));
99
                } catch (NoSuchElementException e2) {
100
                    descriptionElementText = descriptionElement.getText();
101
                }
102
            }
103
        }
104

  
105
        /**
106
         * @return the subList
107
         */
108
        public DescriptionList getSubList() {
109
            return subList;
110
        }
111

  
112
        /**
113
         * @return the descriptionElementContent
114
         */
115
        public BaseElement getDescriptionElementContent() {
116
            return descriptionElementContent;
117
        }
118

  
119
        public String getDescriptionElementText() {
120
            if(descriptionElementContent != null) {
121
                return descriptionElementContent.getText();
122
            } else {
123
                return descriptionElementText;
124
            }
125
        }
126
    }
127

  
128
}
src/test/java/eu/etaxonomy/dataportal/selenium/tests/reference/SpecimensTopDownViewTest.java
22 22

  
23 23
import eu.etaxonomy.dataportal.DataPortalSite;
24 24
import eu.etaxonomy.dataportal.DrupalVars;
25
import eu.etaxonomy.dataportal.elements.BaseElement;
26
import eu.etaxonomy.dataportal.elements.DescriptionList;
27
import eu.etaxonomy.dataportal.elements.Dynabox;
25 28
import eu.etaxonomy.dataportal.junit.CdmDataPortalTestBase;
26 29
import eu.etaxonomy.dataportal.junit.DataPortalContextSuite.DataPortalContexts;
27 30
import eu.etaxonomy.dataportal.pages.TaxonPage;
......
46 49
    @Test
47 50
    public void test1() throws MalformedURLException {
48 51
        TaxonPage p = new TaxonPage(driver, getContext(), glenodinium_apiculatum_t, "specimens");
49
        WebElement specimensTable = p.getDataPortalContent().getElement().findElement(By.cssSelector("#specimans table.specimens"));
52
        WebElement specimensTable = p.getDataPortalContent().getElement().findElement(By.cssSelector("#specimens table.specimens"));
50 53
        List<WebElement> rows = specimensTable.findElements(By.tagName("tr"));
51 54
        assertEquals(3, rows.size());
52 55
        assertEquals("Epitype: Germany, Berlin, 52°31'1.2\"N, 13°21'E, 28.3.2016, D047 (CEDiT 2017E68).", rows.get(0).getText());
53 56
        assertEquals("Isolectotype: Germany, Berlin, 52°31'1.2\"N, 13°21'E, 28.3.2016, D047 (M M-0289351).", rows.get(1).getText());
54 57
        assertEquals("Lectotype: -title cache generation not implemented-", rows.get(2).getText());
55 58

  
59
        Dynabox dynabox1 = new Dynabox(rows.get(0).findElement(By.className("dynabox")), driver);
60
        BaseElement dynaboxContent = dynabox1.click();
61
        List<WebElement> contentElements = dynaboxContent.getElement().findElements(By.xpath("./child::div"));
62
        assertEquals(3, contentElements.size());
63
        // 1
64
        DescriptionList dl1 = new DescriptionList(contentElements.get(0).findElement(By.tagName("dl")));
65
        assertEquals("PreservedSpecimen", dl1.joinedDescriptionElementText("Record basis:"));
66
        assertEquals("Specimen", dl1.joinedDescriptionElementText("Kind of unit:"));
67
        assertEquals("CEDiT\nCode:\nCEDiT", dl1.joinedDescriptionElementText("Collection:"));
68
        assertEquals("2017E68", dl1.joinedDescriptionElementText("Most significant identifier:"));
69
        assertEquals("2017E68", dl1.joinedDescriptionElementText("Accession number:"));
70
        assertEquals("Epitype (designated by Kretschmann, J., Žerdoner ?alasan, A. & Kusber, W.-H. 20171)",
71
                dl1.joinedDescriptionElementText("Specimen type designations:"));
72
        // 2
73
        assertEquals("gathering in-situ from:", contentElements.get(1).getText());
74
        // 3
75
        DescriptionList dl3 = new DescriptionList(contentElements.get(2).findElement(By.tagName("dl")));
76
        assertEquals("FieldUnit", dl3.joinedDescriptionElementText("Record basis:"));
77
        assertEquals("D047", dl3.joinedDescriptionElementText("Collecting number:"));
78
        assertEquals("2016-03-28", dl3.joinedDescriptionElementText("Gathering date"));
79
        assertEquals("Germany", dl3.joinedDescriptionElementText("Country:"));
80
        assertEquals("Berlin", dl3.joinedDescriptionElementText("Locality:"));
81
        assertEquals("52°31'1.2\"N, 13°21'E\nError radius:\n" +
82
                "20 m\n" +
83
                "Longitude:\n" +
84
                "13.35°\n" +
85
                "Latitude:\n" +
86
                "52.517°", dl3.joinedDescriptionElementText("Exact location:"));
87

  
56 88

  
57 89

  
58 90

  

Also available in: Unified diff