Thursday, April 3, 2025
Home Blog Page 1283

Harvard Neuroscientists and Google DeepMind Create Synthetic Mind in Digital Rat

0

In a powerful collaboration, researchers at Harvard College have joined forces with Google DeepMind scientists to create a man-made mind for a digital rat. Printed in Naturethis revolutionary breakthrough opens new doorways in learning how brains management advanced motion utilizing superior AI simulation methods.

Constructing the Digital Rat Mind

To assemble the digital rat’s mind, the analysis crew utilized high-resolution knowledge recorded from actual rats. The Harvard researchers labored carefully with the DeepMind crew to construct a biomechanically sensible digital mannequin of a rat. Graduate scholar Diego Aldarondo collaborated with DeepMind researchers to coach an synthetic neural community (ANN), which serves because the digital mind, utilizing the highly effective machine studying approach deep reinforcement studying.

The neural community was educated to make use of inverse dynamics fashions, that are believed to be employed by our brains for guiding motion. These fashions allow the mind to calculate the required trajectory and translate it into motor instructions for reaching a desired movement, reminiscent of reaching for a cup of espresso. The digital rat’s neural community realized to generate the required forces to supply a variety of behaviors, together with these not explicitly educated, by utilizing reference trajectories derived from actual rat knowledge.

As Ölveczky famous, “DeepMind had developed a pipeline to coach biomechanical brokers to maneuver round advanced environments. We merely did not have the sources to run simulations like these, to coach these networks.” The collaboration was “improbable,” he added, emphasizing the essential position performed by the DeepMind scientists in realizing this breakthrough.

The result’s a digital mind able to controlling a biomechanically sensible 3D rat mannequin inside a classy physics simulator, carefully mimicking the actions of an actual rodent.

Potential Functions

The digital rat with its synthetic mind presents a novel method for probing the neural circuits liable for advanced behaviors. By learning how the AI-generated mind controls the digital rat’s actions, neuroscientists can acquire priceless insights into the intricate workings of actual brains.

This breakthrough may additionally pave the best way for engineering extra superior robotic management techniques. As Ölveczky suggests, “Whereas our lab is eager about basic questions on how the mind works, the platform may very well be used, as one instance, to engineer higher robotic management techniques.” By understanding how the digital mind generates advanced behaviors, researchers might be able to develop extra refined and adaptive robots.

Maybe most excitingly, this analysis might allow a brand new discipline of “digital neuroscience,” the place AI-simulated animals function handy and absolutely clear fashions for learning the mind, even in illness states. These simulations may present an unprecedented window into the neural mechanisms behind numerous neurological circumstances, doubtlessly resulting in new therapy methods.

Subsequent Step: Extra Digital Rat Autonomy

Constructing upon this groundbreaking work, the researchers plan to present the digital rat extra autonomy to unravel duties akin to these encountered by actual rats. As Ölveczky explains, “From our experiments, we’ve got a whole lot of concepts about how such duties are solved, and the way the educational algorithms that underlie the acquisition of expert behaviors are applied.”

By granting the digital rat extra independence, the scientists can take a look at their theories in regards to the studying algorithms that allow the acquisition of latest expertise. This might present priceless insights into how actual brains be taught and adapt to new challenges.

Finally, the objective is to advance our understanding of how actual brains generate advanced conduct. “We wish to begin utilizing the digital rats to check these concepts and assist advance our understanding of how actual brains generate advanced conduct,” Ölveczky states. By persevering with to refine and increase upon this revolutionary method, neuroscientists and AI researchers can work collectively to unravel the mysteries of the mind and create extra clever, adaptable techniques.

The Startup Journal Sustainable Provide Chains: Methods for Success with Dudley Gann

0

Trendy society as we all know it might not exist with out the evolution of in the present day’s subtle provide chains. We are able to have practically something delivered straight to our doorstep—usually inside days and even hours—however the advantages of contemporary provide chains prolong past simply pace and comfort. From the contemporary produce stocked in our native supermarkets to the fast supply of life-saving medicines, fashionable provide chains have change into the spine supporting our every day lives. 

supply chain transport

Nonetheless, regardless of these advantages, in the present day’s provide chains usually fall brief when it comes to sustainability, with the relentless concentrate on price discount and effectivity usually resulting in environmentally detrimental practices. The reliance on fossil fuels for transportation and energy-intensive manufacturing processes has considerably elevated greenhouse fuel emissions, and the worldwide nature of provide chains implies that merchandise usually journey hundreds of miles earlier than reaching shoppers, contributing to environmental degradation. On high of that, unsustainable practices similar to over-reliance on non-renewable assets, insufficient waste administration, and poor labor situations in components of the availability chain additional exacerbate the issue.

With a purpose to mitigate these unfavourable impacts, integrating environmental sustainability into provide chain administration might be essential. By adopting sustainable practices, companies can cut back their environmental footprint and preserve assets all through their provide chains. Whereas doing so will clearly profit the surroundings, it may well additionally improve long-term enterprise viability, decreasing the rising dangers related to issues similar to useful resource shortage and regulatory compliance. 

“Integrating sustainability into provide chain administration is crucial for constructing a future-ready enterprise, stated Dudley Gann, managing director of the inexperienced know-how consulting agency EcoVision Options. “Corporations have to be adopting sustainable practices, not solely to cut back their environmental influence, but in addition with a view to see larger effectivity and guarantee their enterprise and model have long-term resilience.”

Gann’s expertise in inexperienced know-how, gained by means of years of working in renewable vitality startups and established inexperienced tech corporations, impressed him to launch his personal consultancy in 2018. EcoVision Options offers strategic steerage to companies looking for to combine environmental sustainability into their operations, using his experience to drive each ecological and financial advantages for his purchasers. 

Conventional provide chain sustainability challenges

Conventional provide chain practices, whereas environment friendly and cost-effective, usually have important unfavourable impacts on the surroundings. One of the crucial distinguished points is the excessive degree of greenhouse fuel emissions ensuing from intensive transportation and energy-intensive manufacturing processes. Merchandise usually journey hundreds of miles all through the varied phases of the availability chain, contributing to carbon dioxide emissions and air air pollution.

Useful resource depletion is one other main concern. Standard provide chains usually rely closely on non-renewable assets similar to fossil fuels, metals, and minerals. The extraction and processing of those supplies can result in a complete host of ecological issues, together with habitat destruction, soil degradation, and water air pollution. Moreover, the manufacturing processes related to conventional provide chains sometimes generate massive quantities of waste, together with hazardous byproducts that may contaminate air and water sources.

Waste administration inside conventional provide chains can be problematic. Many merchandise are designed with a linear lifecycle, the place they’re disposed of after use somewhat than being recycled or repurposed. This linear method contributes to rising landfills and elevated air pollution. These insufficient waste administration practices can even result in important environmental hazards similar to plastic air pollution in oceans and poisonous chemical leaks.

A variety of strategic choices

Transitioning to sustainable provide chains is crucial for mitigating these impacts and guaranteeing the long-term well being of our planet, and it additionally presents quite a few benefits that transcend environmental influence. Companies have the power to attain important price financial savings whereas additionally benefiting from enhanced model fame and loyalty in a world that’s more and more stuffed with eco-conscious shoppers. On high of that, as environmental rules change into extra stringent throughout the globe, taking a proactive method reduces the chance of fines and authorized points down the highway. 

Dudley Gann explains, “Sustainable provide chains are constructed on a basis of three ideas—environmental stewardship, social accountability, and financial viability. Every performs a significant function in guaranteeing that companies function in a manner that minimizes their environmental influence whereas sustaining long-term profitability.” 

Beneath, Gann has shared three case research of corporations his agency has supplied consulting companies to, providing examples of profitable inexperienced provide chain initiatives:

Implementation of Renewable Power Sources in Manufacturing Services

“A producer within the electronics trade approached us cautious about impending regulatory modifications of their sector. We suggested the mixing of renewable vitality sources throughout their manufacturing amenities, offering steerage on investments in photo voltaic panels and wind generators. This enabled the corporate to drastically cut back its reliance on fossil fuels, slicing greenhouse fuel emissions by over 40%, not solely assembly however exceeding their goal. This ensured it was shielded from the environmental rules on the horizon and expensive authorized ramifications that would observe.”

Adoption of Eco-Pleasant Packaging Supplies and Design

“A cosmetics firm found of their market analysis that their goal buyer demographic extremely prioritized manufacturers that have been devoted to sustainable practices. They approached us for strategic steerage on find out how to higher align with these values, and we suggested them on overhauling their packaging method by adopting eco-friendly supplies and design practices. This included each changing conventional plastic packaging with biodegradable and recycled supplies, in addition to redesigning their packaging to be extra compact with a view to cut back waste and optimize transportation logistics. The modifications have been nicely met by shoppers, boosting buyer loyalty and enhancing the corporate’s market place as a pacesetter in sustainability.”

Integration of Reverse Logistics for Product Recycling and Reuse

“A serious retailer within the attire trade sought us out to sustainably lower prices of their provide chain. They have been initially anticipating to obtain recommendation on supplies and logistics, however our analysis discovered that implementing a complete reverse logistics system to facilitate product recycling and reuse could be a extra impactful alternative. This initiative would enable prospects to return used clothes to them, which have been then sorted, processed, and refurbished for resale. By integrating reverse logistics, they have been in a position to open up a completely new income stream whereas additionally diverting  a major quantity of textile waste from landfills, selling a round economic system throughout the vogue trade.”

Overcoming complexity and challenges

Gann emphasizes that whereas these summaries are illustrative, they don’t totally seize the complexities companies should navigate with a view to transition to extra sustainable provide chains. “It’s essential to acknowledge that these initiatives characterize the end result of months, and generally years, of devoted collaboration between our group and the companies we associate with to attain these outcomes.”

Certainly, transitioning to sustainable provide chains presents a variety of challenges and limitations that companies should navigate to attain long-term success. One major impediment is the lack of expertise and buy-in from stakeholders, as many key decision-makers within the course of might not totally perceive the advantages of sustainable practices or view them as non-essential. To be truthful to those views, preliminary funding prices and monetary constraints can pose important limitations, as upfront bills for renewable vitality installations and eco-friendly supplies will be substantial. Moreover, the worldwide nature of provide chains has launched a brand new layer of complexity to regulatory compliance, requiring an intensive understanding of regional rules that may be daunting, particularly for smaller companies with out massive authorized groups at their disposal. 

Tailoring methods to suit particular organizational wants ensures they’re adequately addressing their very own distinctive challenges and alternatives. Fairly than taking a one-size-fits-all method, Gann advises leaders to take a look at what sustainability initiatives will maximize influence whereas additionally gelling finest with their enterprise mannequin. Regardless, the core precept stands: a sustainability-conscious enterprise is poised for longevity, prioritizing long-term considering over short-term features. 

“Sustainable provide chain methods should be extremely customized to ensure that them to be efficient,” Gann stated. “Nonetheless, each enterprise that commits to implementing sustainable provide chain initiatives might be future-proofing their enterprise and guaranteeing its long-term development.”

6 causes to purchase a Sony TV over a Samsung TV

0

Key Takeaways

  • Prime good TV manufacturers Samsung, Sony, and LG push innovation, however Sony has particularly adopted top-tier shows with spectacular OLED panels, Dolby Imaginative and prescient, and nice back-lighting.
  • Sony affords higher immersive sound and helps PlayStation gaming.
  • Sony’s Google TV interface affords a extra content-rich and intuitive UI than Samsung fashions.



With Samsung, Sony, and LG because the three high manufacturers within the good TV market, competitors does certainly appear to breed innovation. These electronics giants are consistently vying for the highest spot by enhancing on present applied sciences and integrating options from others. So, as a client, it is all the time price checking for the most recent improvements and updates to see whether or not you must swap out your model loyalty.

Samsung and Sony specifically have leveled up lately, becoming a member of LG in what was as soon as an unique marketplace for high-end OLED TVs. With each firms now providing a variety of good TVs that showcase each vivid colours and beautiful distinction, distinctions between the 2 relaxation within the particulars.

Whether or not you are a Samsung or Sony loyalist, or solely now venturing into the world of Good TVs and on the lookout for a facet, it is price taking a step again and analyzing these electronics giants from a large angle lens.


Whether or not you are a Samsung or Sony loyalist, or solely now venturing into the world of Good TVs and on the lookout for a facet, it is price taking a step again and analyzing these electronics giants from a large angle lens. There appears to be little to separate these two, however as somebody who has lengthy aligned with Samsung, shopping for not solely the model’s TV, but in addition a soundbar, speaker set and smartphone, I am beginning to consider switching sides.

As an avid PlayStation gamer and client of content material, I need to have a look at which model offers me essentially the most immersive and intuitive expertise.

Associated

Why Sony’s Bravia Core is ideal for videophiles

Spoiler: It has to do with TV built-in 4K.

1 Sony has extra spectacular shows

Prime tier shade and distinction, plus Dolby Imaginative and prescient

Sony 4k hdr tv featuring a snow capped mountain landscape.


Admittedly, there is not an enormous discrepency between high quality among the many high tier TVs. In the previous few years, each firms dove into the OLED market, and whereas Samsung appeared to withstand at first, the corporate ultimately joined because it realized the spectacular LG know-how was too good to disregard.

Reveals are merely so much darker currently, which means you want a top quality good TV to get pleasure from them.

These fashions mix each firms’ penchant for brightly coloured screens with the hyper-local dimming high quality of OLED panels, permitting these TVs to attain the deepest blacks whereas nonetheless sustaining spectacular distinction. It is definitely a reasonably vital attribute for anybody like me who commonly watches status TV on HBO, particularly something involving dragons. Reveals are merely so much darker currently, which means you want a top quality good TV to get pleasure from them.


Associated

6 tips for making your TV display appear brighter

As a cinephile, this is how I sort out a hard-to-see display now that extra TV exhibits are of their “darkish” period.

By way of decision, each supply mainstream 4K and high-end 8K TVs that also appear to be a little bit of an indulgence right now, with little content material filmed in that detailed decision.

Nonetheless, the sting does go to Sony in each the LED and OLED classes, primarily as a result of its Cognitive Processor XR delivering devoted, colourful photos. Its Mini LED TVs come as shut as might be to matching the dimming energy of OLED TVs with out really having individually dimmed pixels; each kinds nonetheless boast large shade, backed by its XR Triluminos Professional.

The sting goes to Sony in each the LED and OLED classes, primarily as a result of its Cognitive Processor XR delivering devoted, colourful photos.


Moreover, Sony helps Dolby Imaginative and prescient and Samsung doesn’t. Samsung continues to stay with HDR10 and HDR10+ formatting on its TVs, opting out of paying Dolby for the rights to the know-how. Most viewers really feel that whereas the variations could also be small, the Dolby Imaginative and prescient has a better ceiling for brightness ranges, permitting it to provide a extra colourful picture with the correct content material.

Samsung is credited with ‘quantum’ branding, which has ushered in an period of not simply QLED TVs, but in addition Neo QLED and QD-OLED TVs. It is loads of letters. Quantum dots are a advertising time period for a know-how that beforehand existed, and whereas placing ‘quantum’ in entrance of all the pieces could seem enjoyable, it is led to loads of convoluted and complicated acronyms. Thanks, Samsung.

Associated

QLED vs OLED TVs: What’s the actual distinction and what does it even imply?

TV purchasing is enjoyable, however jargon heavy. We break down the variations between QLED and OLED know-how, how they work, and which sort is best for you.

2 Sony affords action-packed experiences

Immersive gaming, sports activities viewing, and motion film binging

sony x9 4k tv featuring a soccer team running hand-in-hand across a pitch.


Sony and Samsung additionally share similarities with regards to energy and processing. Each firms supply TVs with their very own native 120Hz refresh charge, which they then enhance with their proprietary synthetic know-how. That larger refresh charge is essential for having fun with newer, dynamic content material, together with video video games and motion movies, in addition to dwell sporting occasions.

A better refresh charge is essential for having fun with newer, dynamic content material, together with video video games and motion movies, in addition to dwell sporting occasions.

Each Samsung and Sony make new TVs that upscale older content material, enhancing the standard of any variety of previous sitcoms that I constantly play on a Saturday afternoon.

Whereas high-end good TVs from Sony and Samsung give attention to delivering one of the best picture, they’re going to grow to be more and more good at delivering high quality sound as nicely. Each might be higher supported by a suitable soundbar; Samsung’s Q-Symphony tech, for instance, can use the sound from each the soundbar and TV to extend the immersive expertise.


Proper out the field, Sony squeezes out Samsung with some spectacular audio options, together with Sony’s Acoustic Floor Audio+ know-how. This function analyzes your atmosphere to optimize how sound is distributed within the room for essentially the most immersive aural sensation.

Sony strongly helps PlayStation, whereas Samsung is aligned extra with Xbox.

So far as gaming goes, Sony strongly helps PlayStation, whereas Samsung is aligned extra with Xbox, providing Recreation Cross by the Samsung Gaming Hub. That isn’t to say you may’t benefit from the reverse console on every TV — each firms boast gaming options to reinforce high quality, together with altering the refresh charges and display — however when you have a PS5 and need to get essentially the most out of it, Sony’s high TVs embody automated HDR tone mapping and style image mode for the final word gaming expertise.


Sony’s high TVs embody automated HDR tone mapping and style image mode for the final word gaming expertise.

Associated

I attempted the PS5 DualSense and Edge controllers to see which is definitely higher

Sony affords a totally fledged professional controller, however is it price getting over the common DualSense? I’ve used each and may break it down.

3 Google TV is intuitive

Google TV is for content material connoisseurs

Google TV interface featuring Disney+'s The Mandalorian.

Google 

In terms of the way you work together with the TV, Sony has principally moved on from Android TV (it is nonetheless round on older fashions) and is now absolutely immersed in Google TV, an intuitive built-in system seemingly designed for many who watch loads of content material throughout a number of companies. It affords suggestions and does a worthy job preserving disparate content material collectively. Newer fashions have Google Assistant in-built, integrating simply with different Google gadgets. Whereas Samsung TVs have used Google Assistant, they may now not function it on forthcoming fashions.


Samsung, nevertheless, runs Tizen OS, which has been their customary system for a few years. It is easy to navigate and quick, however pretty fundamental; it appears constructed from days previous with far fewer streaming companies, subscriptions and apps. The battle for TV supremacy is more and more tight and consistently evolving. Trustworthy shoppers of both Samsung or Sony have lots to brag about, nevertheless it’s nonetheless price what the opposite facet is doing to maintain the market revolutionary and thrilling.

Google TV is solely higher suited to navigating all of the out there choices.


I can not assist however lean in the direction of Sony, significantly as somebody who not solely enjoys the most recent video games on a PlayStation 5, but in addition as somebody who watches loads of exhibits and films from a variety of streaming companies. Google TV is solely higher suited to navigating all of the out there choices. Sony’s high Mini-LED and OLED TVs obtain the brightest colours and deepest blacks for a devoted and memorable leisure expertise.

Associated

5 important issues to learn about your TV’s movement smoothing function

Including in synthetic frames to fill content material gaps, administrators and Tom Cruise need you to show it off, nevertheless it has its place.

4 Calibration innovation

Sony strives for cinematic constancy

Sony bravia x9 tv featuring the Mona Lisa.

Each TV model is aiming to carry the theater expertise to your lounge, partly by robotically recreating content material precisely because the filmmakers envisioned. Sony’s improvements over time show it’s taking critically this job, from Netflix Calibrated, a preset that optimizes content material on the streaming service, to BRAVIA CORE Calibrated, an automated setting for its personal platform. Bravia Core, specifically, goals at bringing the IMAX expertise to your property TV, optimizing each picture and sound.


Whereas Samsung does have Filmmaker Mode, a well-liked function Sony has opted out of, the longer term is extra formidable for the Japanese electronics big. It needs to work with filmmakers in order that its TVs use dynamic metadata inside content material to robotically modify the image settings. All of this goes into making content material at residence extra pleasant, which incorporates embracing vivid colours, fluid movement, and stark distinction each time the display is darkish. And there is loads of darkish content material on the market.

Associated

This underused setting is the best way to level-up your good TV

Good TVs are fairly good, however calibration makes them look and carry out even higher.

5 Spectacular sound options

Superior audio improvements

Sony Bravia XR TV range 2023 watched by woman and young boy

Sony

Each high TV model appears to be leveling up their audio sport as a way to add immersive sound to the immersive visuals. Sony, specifically, has finished so much, and even in case you are with out a soundbar, you are still arrange for a top quality listening expertise.


Sure TVs use built-in audio system to create encompass sound, firing horizontally in addition to vertically. Sony additionally options Acoustic Floor Audio+ on some fashions, through which audio system behind are activated, altering and transferring with the picture on the TV in order that sound feels particular and noteworthy. In the meantime, the Voice Zoom 2 function isolates and boosts dialogue on display in order that it may be heard clearly. Sony boasts Acoustic Auto Calibration, an intuitive function that analyzes the format of the speedy space as a way to optimize audio settings.

You can begin with a soundbar and slowly improve your property theater system over time.


Sony TVs additionally assist Dolby Atmos, which might supply spatial sound to fill the room. All of those audio perks are enhanced with a suitable Sony sound system. Even should you’re not prepared to purchase a large set, due to Sony’s many audio choices, you can begin with a soundbar and slowly improve your property theater system over time.

Associated

These are the 5 methods I make my TV sound louder

Your TV may wrestle with sound, however your ears do not need to.

6 Sony’s TV cams supply comfort

The digital camera may also help, should you let it

A family watching a Sci-Fi movie on Sony's new Bravia TV .

Sony

Newer Sony TVs embody a digital camera, referred to as the BRAVIA CAM, on the middle of the highest of the unit, and its inclusion is supposed to assist with ease of use and to enhance the general cinematic expertise. For instance, the cam will detect the place you might be sitting and robotically modify each picture and audio settings, optimizing each based mostly in your location with the intention to greatest benefit from the content material on display. For video, the brightness will modify so that you’re not overwhelmed should you’re too shut, or watching a dim display should you’re farther away. For audio, in the meantime, the TV will enhance the left or proper speaker based mostly in your place relative to the middle. It can additionally enhance the dialogue based mostly in your place, with the intention to hear everybody talking.


Associated

I exploit these 4 Tizen options to get essentially the most out of my Samsung TV

Tizen is not the flashiest TV working system, nevertheless it does proceed to innovate with some helpful options.

The BRAVIA CAM can even detect gestures with the intention to management the TV with hand actions. It can additionally go into power-saving mode when you’re not in entrance of the TV. In the event you’re slightly unsettled together with your TV understanding precisely the place you might be within the room, this perk may not be for you. Nevertheless, should you’re all about comfort, then it is but one more reason to go for a Sony TV.

Sony and Samsung are among the many high TV producers, and whereas they provide loads of comparable experiences and applied sciences, the important thing variations are what’s going to enable you determine the place your model allegiance lies.

Associated

5 causes to purchase a Samsung TV over a Sony TV

An emphasis on brightness and sweetness has me leaning Samsung.

New options come to Apple companies this fall

0

Greatest Mom’s Day reward concepts for that particular woman

0

Mom’s Day is true across the nook, and I do know that almost all of us want to make our mothers or moms of our youngsters really feel further particular on at the present time. Certainly, there are lots of methods to take action, together with a particular evening out, a dinner, roses, jewellery, and extra, however I consider that we are able to attempt to suppose a bit exterior the field and attempt to give them one thing that they could discover helpful, which is why we now have made this checklist that can assist you discover the very best reward to your particular woman.



For health lovers

Apple Watch Series 9 Graphite PBI

Apple Watch Sequence 9

$299 $399 Save $100

Apple Watch Sequence 9 is the most recent smartwatch from the Cupertino big. It presents boasts a full day of battery life, a sooner processor with on-device AI, and modern gesture-based performance. You may select from GPS or Mobile choices and choose both a 41mm or 45mm mannequin.

Many people love the additional weight that comes with burgers, pizza, and extra, however there are extra who want to reside a wholesome life. They watch what they eat and are all the time searching for methods to remain energetic. We’ve got included some good choices that may make their coaching higher, extra environment friendly, and extra enjoyable.

Our best choice comes from Apple, because the Apple Watch Sequence 9 is among the finest smartwatches you will get as we speak. It comes with a large 25 p.c low cost, which suggests you will get yours for simply $299. This feature usually goes for $399, that means you get to attain $100 in on the spot financial savings. And they’ll go nice with a brand new pair of AirPods Professional, now going for $179 due to a really enticing 28 p.c low cost.


Different glorious choices embody:

For artists

PBI-iPad-Air-5-Space-Grey-1

Apple iPad Air (2022)

$500 $599 Save $99

The iPad Air fifth era comes with a extra highly effective M1 chip inside, which supplies extra efficiency and effectivity. Although the design stays unchanged, it helps Apple Pencil, comes with an M1 chip, and proves to be a robust system to suit all your wants.

I’ve seen how these loving fingers also can create a number of the finest artwork I’ve seen, and there are lots of methods you possibly can encourage their expertise, beginning with a drawing pill. After all, there are lots of choices to select from, however I’ll strongly suggest the present iteration of the iPad Air, because it comes with an M1 processor below the hood and sufficient energy to assist them create something they need.

The fifth era iPad Air may be bought for as little as $500 due to the most recent $99 low cost. It comes with a ten.9-inch Liquid Retina Show, WiFi-only help, and 64GB space for storing. After all, you too can get the 256GB storage variant or the LTE-enabled mannequin if you wish to spend a bit extra. One of the best half is that additionally, you will be capable to take notes, stream your favourite content material, browse the net, and extra.


Different glorious alternate options:

For music lovers

Product Image of Sony WH-1000XM4

Sony WH-1000XM4

$278 $348 Save $70

The Sony WH-1000XM4 function a snug design for lengthy hours of use, as much as 30 hours of continuous music, energetic noise canceling, and extra.

Good music will all the time make any state of affairs higher, and there are lots of methods to assist your mother maximize her audio experiences. First up, we now have a pleasant number of headphones to assist your mother get pleasure from her favourite tunes with out disturbing anybody round her. It’s additionally helpful when attempting to observe your favourite reveals in a crowded place. So our high decide comes from Sony, because the Sony WH-1000XM4 Wi-fi Premium Noise Canceling Overhead Headphones are actually out there for simply $278, due to a 20 p.c low cost. These are nonetheless a number of the finest headphones you will get available on the market, even when there’s a brand new model available on the market.

The Sony WH-1000XM5 are cool, and so they now go for $348 with 13 p.c financial savings, however they nonetheless can’t match every little thing that comes with their earlier iteration. However, I’ve all the time believed that one of the simplest ways to get pleasure from music is to do it in your house, so you may additionally wish to contemplate a turntable, the place you’ll find some good choices from Fluance and Audio-Technica up for grabs.


61yYyMtayXL._AC_SL1200_

Common Audio UAFX Astra Modulation Pedal

$357 $399 Save $42

Probably the greatest modilation pedals you will get available on the market.

And in case your mother or your spouse likes to create music, there are a few concepts that you just would possibly love, beginning with the Common Audio UAFX Astra Modulation Pedal, which now sells for $357 due to a really compelling 11 p.c low cost. Common Audio makes a number of the finest pedals for these concerned with recording their very own music, which is why you must also contemplate testing the Del-Verb Atmosphere Companion pedal and different good merchandise which might be presently on sale on Common Audio. Additionally, you will discover huge 50 p.c financial savings on most plug-ins, with some costs dropping to below $50. One of the best half is that these offers will probably be out there till Might twenty eighth, so you’ve got greater than sufficient time to test them out.

Different superb present concepts:

Video Friday: Drone vs. Flying Canoe

0

Video Friday is your weekly choice of superior robotics movies, collected by your folks at IEEE Spectrum robotics. We additionally submit a weekly calendar of upcoming robotics occasions for the following few months. Please ship us your occasions for inclusion.

RoboCup 2024: 17–22 July 2024, EINDHOVEN, NETHERLANDS
ICRA@40: 23–26 September 2024, ROTTERDAM, NETHERLANDS
IROS 2024: 14–18 October 2024, ABU DHABI, UAE
ICSR 2024: 23–26 October 2024, ODENSE, DENMARK
Cybathlon 2024: 25–27 October 2024, ZURICH

Take pleasure in right now’s movies!

There’s a Canadian legend a couple of flying canoe, due to course there’s. The legend includes drunkenness, a celebration with some women, swearing, and a pact with the satan, due to course it does. Happily for the drone on this video, it wants none of that to efficiently land on this (almost) flying canoe, just a few high-friction shock absorbing legs and even handed utility of reverse thrust.

[ Createk ]

Thanks, Alexis!

This paper summarizes an autonomous driving challenge by musculoskeletal humanoids. The musculoskeletal humanoid, which mimics the human physique intimately, has redundant sensors and a versatile physique construction. We rethink the developed {hardware} and software program of the musculoskeletal humanoid Musashi within the context of autonomous driving. The respective parts of autonomous driving are performed utilizing the advantages of the {hardware} and software program. Lastly, Musashi succeeded within the pedal and steering wheel operations with recognition.

[ Paper ] by way of [ JSK Lab ]

Thanks, Kento!

Sturdy AI has been kinda quiet for the final short time, however their Carter robotic continues to enhance.

[ Robust AI ]

One of many key arguments for constructing robots which have comparable kind components to human beings is that we are able to leverage the large human information for coaching. On this paper, we introduce a full-stack system for humanoids to be taught movement and autonomous abilities from human information. We show the system on our custom-made 33-degrees-of-freedom 180 centimeter humanoid, autonomously finishing duties reminiscent of carrying a shoe to face up and stroll, unloading objects from warehouse racks, folding a sweatshirt, rearranging objects, typing, and greeting one other robotic with 60-One hundred pc success charges utilizing as much as 40 demonstrations.

[ HumanPlus ]

We current OmniH2O (Omni Human-to-Humanoid), a learning-based system for whole-body humanoid teleoperation and autonomy. Utilizing kinematic pose as a common management interface, OmniH2O allows numerous methods for a human to manage a full-sized humanoid with dexterous fingers, together with utilizing real-time teleoperation by means of VR headset, verbal instruction, and RGB digital camera. OmniH2O additionally allows full autonomy by studying from teleoperated demonstrations or integrating with frontier fashions reminiscent of GPT-4.

[ OmniH2O ]

A collaboration between Boxbot, Agility Robotics, and Sturdy.AI at Playground World. Be certain that and watch till the tip to listen to the roboticists within the background react when the demo works in a really roboticist approach.

::clap clap clap:: yaaaaayyyyy….

[ Robust AI ]

Using drones and robotic gadgets threatens civilian and army actors in battle areas. We began trials with robots to see how we are able to adapt our HEAT (Hostile Atmosphere Consciousness Coaching) programs to this new actuality.

[ CSD ]

Thanks, Ebe!

Learn how to make humanoids do versatile parkour leaping, clapping dance, cliff traversal, and field pick-and-move with a unified RL framework? We introduce WoCoCo: Entire-body humanoid Management with sequential Contacts

[ WoCoCo ]

A choice of wonderful demos from the Studying Methods and Robotics Lab at TUM and the College of Toronto.

[ Learning Systems and Robotics Lab ]

Harvest Automation, one of many OG autonomous cellular robotic corporations, hasn’t up to date their web site since like 2016, however some movies simply confirmed up on YouTube this week.

[ Harvest Automation ]

Northrop Grumman has been pioneering capabilities within the undersea area for greater than 50 years. Now, we’re creating a brand new class of uncrewed underwater autos (UUV) with Manta Ray. Taking its title from the large “winged” fish, Manta Ray will function long-duration, long-range missions in ocean environments the place people can’t go.

[ Northrop Grumman ]

Akara Robotics’ autonomous robotic UV disinfection demo.

[ Akara Robotics ]

Scientists have computationally predicted tons of of hundreds of novel supplies that might be promising for brand new applied sciences—however testing to see whether or not any of these supplies could be made in actuality is a gradual course of. Enter A-Lab, which makes use of robots guided by synthetic intelligence to hurry up the method.

[ A-Lab ]

We wrote about this analysis from CMU some time again, however right here’s a fairly good video.

[ CMU RI ]

Aw yiss decide and place robots.

[ Fanuc ]

Axel Moore describes his lab’s work in orthopedic biomechanics to alleviate joint ache with robotic help.

[ CMU ]

The sphere of humanoid robots has grown lately with a number of corporations and analysis laboratories growing new humanoid programs. Nonetheless, the variety of operating robots didn’t noticeably rise. Regardless of the necessity for quick locomotion to shortly serve given duties, which require traversing advanced terrain by operating and leaping over obstacles. To offer an summary of the design of humanoid robots with bioinspired mechanisms, this paper introduces the elemental features of the human operating gait.

[ Paper ]

Analyse einer ausgefeilten Social-Engineering-Kampagne – Sophos Information

0

Innerhalb von 51 Tagen verschickte eine Gruppe von Angreifern, die vermutlich aus Russland stammt, mehr als 2.000 Phishing-E-Mails an quick 800 Unternehmen und Organisationen aus den Bereichen Regierung, Gesundheitswesen, Energie und kritische Infrastrukturen. Die Ziele befanden sich in Großbritannien, Australien, Frankreich, Deutschland, Österreich, Italien sowie in den USA und Niederlanden.

Die E-Mails zeichneten sich durch eine ungewöhnliche, hochgradig personalisierte Technik aus: die Einbettung eines Webseiten-Logos, das von einer eigenen Seite der Zielperson stammt, in die Phishing-Seite selbst. Nach dem Öffnen wurden die Zielpersonen aufgefordert, ihre Passwörter in die Anmeldeseite der scheinbar eigenen Webseite einzugeben. Anschließend schleusten die Angreifer die gestohlenen Passwörter in ihre Telegram-Kanäle ein.

Offenbar haben die Angreifer das Wissen über on-line nachvollziehbare Communities ausgenutzt, wodurch das Sophos-Staff auf die Kampagne aufmerksam wurde. Andrew Brandt von Sophos X-Ops erhielt zunächst eine E-Mail von einem der Angreifer, als er für die örtliche Schulratswahl in Boulder, Colorado, USA, kandidierte, wobei der Angreifer vorgab, einer seiner Mitkandidaten zu sein. Als die ersten BEC-E-Mails (Enterprise E mail Compromise) fehlschlugen, wechselten die Angreifer zu Phishing-E-Mails und schickten Andrew eine E-Mail mit einem Anhang, der die Anmeldeseite für seine scheinbar persönliche Kampagnen-Web site enthielt.

„Die BEC-Kampagne warfare zwar recht einfach gestrickt, zeigt jedoch, dass die Angreifer geschickt eine Analyse der on-line verfügbaren, sozialen Kontakte im Umfeld der Zielperson ausnutzen, um möglichst realistisch zu wirken“, so Andrew Brandt. „Bedeutend ausgefeilter warfare hingegen der nachfolgende Social-Engineering-Versuch. Die Verwendung von grafischen Elementen, die die Zielperson selbst nutzt, zeigt die Raffinesse, mit der Betrüger ihr Attacken mittlerweile vorbereiten.“

Detaillierte Einblicke in die Social-Engineering-Kampagne finden Sie im englischsprachigen Report „From Russia with Love: Credential Theft Assault Makes use of Refined Social Engineering Methods to Goal 800 Organizations Worldwide”.

Which is a Higher Library?

0

Introduction

Tensorflow and Keras are well-known machine studying frameworks for information scientists or builders. Within the upcoming sections we’ll look at the professionals, downsides, and variations between these libraries. We may even discover Tensorflow vs Keras on this article.

Overview

  • Find out about Keras vs TensorFlow.
  • Learn the way they differ from one another.
  • Discover out which is extra fitted to you.
  • Be taught the professionals and cons of each these frameworks.

What’s TensorFlow?

TensorFlow is a strong end-to-end Deep Studying framework. TensorFlow APIs are organized in a hierarchical construction, with higher-level APIs constructing on lower-level APIs. Machine studying researchers use low-level APIs to create and check new algorithms. 

What’s Keras?

Keras is a Python-based deep studying API, Keras is easy, but not simplistic. Keras decreases the cognitive load on builders, permitting them to give attention to a very powerful points of the issue.

It’s versatile, adhering to the precept of accelerating complexity disclosure: fundamental duties are fast and easy, whereas superior workflows will be achieved by means of clear, incremental steps. It boasts industry-leading efficiency and scalability, and is utilized by organizations equivalent to NASA, YouTube, and Waymo.

TensorFlow vs Keras

Characteristic TensorFlow Keras
Developed By Google Mind François Chollet (now a part of TensorFlow)
API Degree Low-level and high-level Excessive-level
Flexibility Extremely versatile, helps customized operations and layers Much less versatile, primarily for traditional layers and fashions
Ease of Use Steeper studying curve, extra management Person-friendly, easy to implement
Deployment Intensive assist (TensorFlow Lite, TensorFlow Serving) Makes use of TensorFlow for deployment
Efficiency Optimized for efficiency, helps distributed coaching Optimized by means of TensorFlow backend
Group Help Massive group, intensive assets Massive group, built-in inside TensorFlow
Use Case Appropriate for complicated, large-scale initiatives Best for speedy prototyping and experimentation
Knowledge Dealing with Superior information dealing with with tf.information API Simplified information dealing with with built-in strategies
Visualization TensorBoard for superior mannequin visualization Helps TensorBoard

Execs and Cons

Allow us to now discover execs and cons of Tensorflow and Keras.

TensorFlow

Execs:

  • Tensor move outperforms all different prime platforms by way of graph illustration for a given information set.
  • Tensor move presents the good thing about supporting and utilizing a variety of backend software program.
  • It presents the best group assist and can be helpful for debugging sub-graphs.
  • Simple to increase because it lets you create customized blocks to construct on new ideas.

Cons:

  • The tensor move is slower than different platforms of the identical kind.
  • Creating customized layers and operations in will be intricate and time-consuming. For instance, designing a novel convolutional layer for a specialised picture processing process could require vital effort and experience.

Keras

Execs:

  • It’s supposed to be easy and intuitive. It encapsulates most of TensorFlow’s low-level complexity, making it an excellent various for these new to deep studying.
  • It helps speedy prototyping of neural networks, permitting you to experiment with various topologies rapidly.
  • Its code is usually extra succinct and readable than TensorFlow code.
  • It has been included because the official high-level API in TensorFlow from model 2.0, assuring compatibility and synergy between the 2.

Cons:

  • It has little versatility, regardless of its appreciable simplicity. It will not be the best choice for stylish customers who want precise management over all points of their fashions.
  • Customizing layers and processes is hard.

Additionally Learn: High 6 Deep Studying Frameworks You Ought to Know in 2024

Conclusion

TensorFlow excels in flexibility and scalability for intricate initiatives, providing intensive management over neural community design, making it perfect for large-scale purposes like Google’s search algorithms. In distinction, Keras shines with its user-friendly interface, excellent for speedy prototyping, equivalent to rapidly constructing and testing a sentiment evaluation mannequin for buyer evaluations. Now you may make a alternative on which framework to undertake and discover out which is greatest fitted to you – TensorFlow or Keras!

Ceaselessly Requested Questions

Q1. Are there efficiency variations between utilizing Keras and TensorFlow immediately?

A. Efficiency variations between utilizing Keras and TensorFlow immediately are minimal as a result of Keras operations in the end get compiled into TensorFlow computational graphs. 

Q2. Is Keras part of TensorFlow 2.0?

A. Sure, TensorFlow 2.0 integrates Keras as its official high-level API. This helps for a unified expertise for each high-level and low-level operations.

High Takeaways from the Cisco Stay 2024 DevNet Zone: AI, Programmability, and Extra

0

At Cisco Stay in Las Vegas, the DevNet Zone was abuzz with exercise, specializing in the quickly evolving panorama for automation engineering. This 12 months’s occasion highlighted revolutionary methods and instruments which can be driving the {industry}’s shift in direction of AI adoption. Cisco’s dedication to empowering engineers by means of the stack was evident, with AI being positioned as a key pillar of future improvement. As automation engineers, you’ve a pivotal position in leveraging AI to create smarter, extra environment friendly options. Cisco DevNet is right here to help your journey, offering the sources and group it is advisable to succeed.

AI is advanced, however with DevNet, it doesn’t need to be daunting. DevNet AI sources intention to assist convey readability to AI integration, making it accessible for engineers in any respect ranges. In the meantime, programmability typically stays an important focus. DevNet continues to allow IT organizations, constructing on our current foundations in automation and programmability, and now extending into AI. Whether or not it’s by means of our superior AI-powered instruments or our supportive group, we’re devoted to serving to you harness AI’s full potential. As you proceed to innovate, DevNet will probably be by your facet, providing the information and instruments to maintain you on the forefront of the {industry}.

High takeaways from the DevNet Zone at Cisco Stay

  1. AI is remodeling utility improvement and automation with new potentialities
    AI is reshaping how we method writing software program, providing new potentialities and efficiencies. At Cisco Stay, we explored AI’s potential to remodel your tasks with insights into AI-powered programmability, predictive AI, and generative AI.
  2. Automation and programmability allow extensibility throughout domains
    Cisco offers the APIs essential for extensibility in safety, networking, knowledge middle, and collaboration, enabling organizations to construct multi-domain options tailor-made to their enterprise wants.
  3. Expertise is advancing sustainable vitality administration
    Sustainability is a key concern as our use of expertise grows. At Cisco Stay, our viewers realized revolutionary approaches to vitality administration by means of automation and programmability.

Contained in the motion on the DevNet Zone

Technical Classroom Classes

The DevNet Zone at Cisco Stay centered on the newest developments in AI, community programmability and extra. Attendees had the prospect to dive into AI-focused classes, together with our session “GenAI Influence on DevOps and Utility Growth: A Technical Perspective”. This session supplied actionable steering on integrating AI improvements into on a regular basis DevOps workflows, and detailed particular Cisco options out there to assist facilitate this integration. The DevNet Zone was packed edge-to-edge with a powerful overflow into the aisles for a session on YANG Information modeling within the NSO Playground and course of community automation.

CLUS 24 classroom

The Sandbox Arcade

Attendees additionally skilled the enjoyable of our new Sandbox Arcade, the place they had been capable of study our Sandbox choices and tips on how to assemble API calls by way of retro type arcade video games, offering a enjoyable participating option to apply coding abilities. Though our arcade video games had been unique to Cisco Stay attendees, DevNet Sandboxes are open to all. You may run your code on reside infrastructure with our out there Sandboxes to develop and take a look at Cisco APIs, SDKs, and extra.

CLUS 24 sandbox arcade

Workshops

Arms-on studying experiences had been delivered in teacher led workshops, the place many classes had waitlists of 100+. These excessive demand classes included subjects on constructing your first GenAI Assistant, automating community duties, utilizing ThousandEyes API integrations for community insights, and Meraki MX safety. Arms-on studying doesn’t cease on website at Cisco Stay. Discover DevNet’s on-line Studying Labs to maintain constructing your abilities on programmability, GenAI, and extra.

CLUS 24 workshops

Share Your Expertise

Our Share Your Expertise program supplied attendees with a novel alternative to share useful suggestions and enter on our options, APIs, and sources. This enter is extraordinarily useful in impacting our future choices, serving to us to constantly enhance and evolve.

CLUS 24 Share your experience

Key bulletins and session highlights

Cisco Stay 2024 was filled with new bulletins, sharing updates on our AI technique and options, in addition to new improvements in safety, networking, and observability. Right here’s the breakdown of what was most enjoyable and impactful for these of us in networking, improvement and operations.

The primary keynote kicked off robust, the place Cisco introduced a $1 billion funding to increase and develop safe and dependable AI options. These investments will additional assist to allow buyer AI readiness, compute infrastructure, foundational fashions, mannequin improvement, and coaching.

New partnerships from these investments embody:

  • Mistral AI focuses on generative synthetic intelligence and develops new GenAI fashions for companies, combining scientific excellence, an open method and a accountable imaginative and prescient of expertise.
  • Scale AI offers a data-centric, end-to-end platform offering coaching and validation for AI purposes.
  • Cohere offers security-focused frontier giant language fashions (LLMs) and industry-leading Retrieval-Augmented Technology (RAG) capabilities tailor-made to satisfy the wants of enterprises.

Options and developments to discover

  • Nvidia and Cisco’s partnership will ship a streamlined answer for deploying generative AI purposes, with Cisco Nexus HyperFabric AI clusters simplifying infrastructure setup. Unique cloud administration instruments will supply easy deployment and monitoring throughout knowledge facilities and edge websites.
  • Cisco Networking Cloud introduces a unified administration platform for seamless on-prem and cloud operations, that includes SSO, API administration, sustainable networking, and enhanced Digital Expertise Assurance with ThousandEyes, simplifying IT with superior AI and vitality insights.
  • XDR with Meraki MX combine collectively to allow direct telemetry evaluation and correlation, enhancing visibility for Meraki admins and providing speedy menace response capabilities inside the Meraki dashboard.
  • Hypershield help for AMD Pensando DPUs and Intel IPUs delivers an AI-driven, distributed safety structure that spans from cloud to knowledge facilities to the sting, optimizing efficiency and vitality effectivity.
  • Motific, a brand new SaaS product for speedy and trusted supply of GenAI purposes, introduced availability free of charge trial utilization for all prospects, and manufacturing utilization for a restricted set of consumers, with a focused normal availability date of July 31, 2024. Moreover, a brand new collaboration with Mistral AI – a number one giant language mannequin supplier – guarantees to reinforce GenAI assistants with main LLM capabilities.
  • Splunk launched Log Observer for Cisco AppDynamics, an integration that allows customers to ship sooner troubleshooting throughout on-prem and hybrid environments. A brand new AI assistant was additionally introduced for AppDynamics that can allow customers to faucet into insightful steering to make knowledgeable selections.
  • New Cisco AI Certifications are being launched, enabling groups construct wanted abilities to design trendy AI/ML compute and networks.

Cisco Stay 2024 was an unimaginable occasion stuffed with studying, innovation, and group engagement. Whether or not you attended or adopted just about, we hope these takeaways encourage you to proceed exploring AI, sustainability, and programmability in your tasks.

Keep linked with DevNet past Cisco Stay

Don’t miss out on ability constructing alternatives, even when you couldn’t attend in-person. Proceed to develop with our developer group and hold the Cisco Stay spirit alive all 12 months lengthy:

  • Discover our technical sources: Sustain with the evolving AI panorama and evolve your abilities with our AI hub. Discover studying labs, pattern code, and sandboxes to reinforce your abilities and apply your information in real-world situations throughout Cisco’s options. Begin exploring all DevNet content material right here >.
  • Be a part of the group: Join with friends and material specialists in your favourite expertise subjects by becoming a member of DevNet Creators in Webex. Share information, ask questions, and collaborate on tasks. Get entry >
  • Keep linked: Subscribe to our e-newsletter and comply with @CiscoDevNet on LinkedIn, X, and Fb for the newest updates, sources, and alternatives to attach with the DevNet group. Watch DevNet specialists on our YouTube channel share unique insights, tutorials, and updates.

 

Share:

Take a look at-Driving HTML Templates

After a decade or extra the place Single-Web page-Purposes generated by
JavaScript frameworks have
change into the norm
, we see that server-side rendered HTML is turning into
widespread once more, additionally due to libraries similar to HTMX or Turbo. Writing a wealthy internet UI in a
historically server-side language like Go or Java is not simply potential,
however a really enticing proposition.

We then face the issue of methods to write automated checks for the HTML
components of our internet functions. Whereas the JavaScript world has advanced highly effective and subtle methods to check the UI,
ranging in measurement from unit-level to integration to end-to-end, in different
languages we would not have such a richness of instruments accessible.

When writing an internet utility in Go or Java, HTML is often generated
by templates, which comprise small fragments of logic. It’s definitely
potential to check them not directly by end-to-end checks, however these checks
are gradual and costly.

We are able to as a substitute write unit checks that use CSS selectors to probe the
presence and proper content material of particular HTML components inside a doc.
Parameterizing these checks makes it simple so as to add new checks and to obviously
point out what particulars every take a look at is verifying. This method works with any
language that has entry to an HTML parsing library that helps CSS
selectors; examples are offered in Go and Java.

Stage 1: checking for sound HTML

The primary factor we wish to verify is that the HTML we produce is
principally sound. I do not imply to verify that HTML is legitimate in line with the
W3C; it might be cool to do it, however it’s higher to begin with a lot less complicated and sooner checks.
As an illustration, we would like our checks to
break if the template generates one thing like

<div>foo</p> 

Let’s have a look at methods to do it in levels: we begin with the next take a look at that
tries to compile the template. In Go we use the usual html/template bundle.

Go

  func Test_wellFormedHtml(t *testing.T) {     templ := template.Should(template.ParseFiles("index.tmpl"))     _ = templ   }

In Java, we use jmustache
as a result of it is quite simple to make use of; Freemarker or
Velocity are different frequent selections.

Java

  @Take a look at   void indexIsSoundHtml() {       var template = Mustache.compiler().compile(               new InputStreamReader(                       getClass().getResourceAsStream("/index.tmpl")));   }

If we run this take a look at, it is going to fail, as a result of the index.tmpl file does
not exist. So we create it, with the above damaged HTML. Now the take a look at ought to cross.

Then we create a mannequin for the template to make use of. The applying manages a todo-list, and
we are able to create a minimal mannequin for demonstration functions.

Go

  func Test_wellFormedHtml(t *testing.T) {     templ := template.Should(template.ParseFiles("index.tmpl"))     mannequin := todo.NewList()     _ = templ     _ = mannequin   }

Java

  @Take a look at   void indexIsSoundHtml() {       var template = Mustache.compiler().compile(               new InputStreamReader(                       getClass().getResourceAsStream("/index.tmpl")));       var mannequin = new TodoList();   }

Now we render the template, saving the leads to a bytes buffer (Go) or as a String (Java).

Go

  func Test_wellFormedHtml(t *testing.T) {     templ := template.Should(template.ParseFiles("index.tmpl"))     mannequin := todo.NewList()     var buf bytes.Buffer     err := templ.Execute(&buf, mannequin)     if err != nil {       panic(err)     }   }

Java

  @Take a look at   void indexIsSoundHtml() {       var template = Mustache.compiler().compile(               new InputStreamReader(                       getClass().getResourceAsStream("/index.tmpl")));       var mannequin = new TodoList();          var html = template.execute(mannequin);   }

At this level, we wish to parse the HTML and we anticipate to see an
error, as a result of in our damaged HTML there’s a div factor that
is closed by a p factor. There’s an HTML parser within the Go
normal library, however it’s too lenient: if we run it on our damaged HTML, we do not get an
error. Fortunately, the Go normal library additionally has an XML parser that may be
configured to parse HTML (due to this Stack Overflow reply)

Go

  func Test_wellFormedHtml(t *testing.T) {     templ := template.Should(template.ParseFiles("index.tmpl"))     mannequin := todo.NewList()          // render the template right into a buffer     var buf bytes.Buffer     err := templ.Execute(&buf, mannequin)     if err != nil {       panic(err)     }        // verify that the template may be parsed as (lenient) XML     decoder := xml.NewDecoder(bytes.NewReader(buf.Bytes()))     decoder.Strict = false     decoder.AutoClose = xml.HTMLAutoClose     decoder.Entity = xml.HTMLEntity     for {       _, err := decoder.Token()       swap err {       case io.EOF:         return // We're completed, it is legitimate!       case nil:         // do nothing       default:         t.Fatalf("Error parsing html: %s", err)       }     }   }

supply

This code configures the HTML parser to have the precise degree of leniency
for HTML, after which parses the HTML token by token. Certainly, we see the error
message we needed:

--- FAIL: Test_wellFormedHtml (0.00s)     index_template_test.go:61: Error parsing html: XML syntax error on line 4: surprising finish factor </p> 

In Java, a flexible library to make use of is jsoup:

Java

  @Take a look at   void indexIsSoundHtml() {       var template = Mustache.compiler().compile(               new InputStreamReader(                       getClass().getResourceAsStream("/index.tmpl")));       var mannequin = new TodoList();          var html = template.execute(mannequin);          var parser = Parser.htmlParser().setTrackErrors(10);       Jsoup.parse(html, "", parser);       assertThat(parser.getErrors()).isEmpty();   }

supply

And we see it fail:

java.lang.AssertionError:  Anticipating empty however was:<[<1:13>: Unexpected EndTag token [</p>] when in state [InBody], 

Success! Now if we copy over the contents of the TodoMVC
template
to our index.tmpl file, the take a look at passes.

The take a look at, nonetheless, is simply too verbose: we extract two helper capabilities, in
order to make the intention of the take a look at clearer, and we get

Go

  func Test_wellFormedHtml(t *testing.T) {     mannequin := todo.NewList()        buf := renderTemplate("index.tmpl", mannequin)        assertWellFormedHtml(t, buf)   }

supply

Java

  @Take a look at   void indexIsSoundHtml() {       var mannequin = new TodoList();          var html = renderTemplate("/index.tmpl", mannequin);          assertSoundHtml(html);   }

supply

Stage 2: testing HTML construction

What else ought to we take a look at?

We all know that the seems of a web page can solely be examined, finally, by a
human how it’s rendered in a browser. Nonetheless, there’s typically
logic in templates, and we would like to have the ability to take a look at that logic.

One is likely to be tempted to check the rendered HTML with string equality,
however this system fails in apply, as a result of templates comprise numerous
particulars that make string equality assertions impractical. The assertions
change into very verbose, and when studying the assertion, it turns into troublesome
to grasp what it’s that we’re attempting to show.

What we want
is a way to claim that some components of the rendered HTML
correspond to what we anticipate, and to ignore all the main points we do not
care about.
A technique to do that is by working queries with the CSS selector language:
it’s a highly effective language that enables us to pick the
components that we care about from the entire HTML doc. As soon as we have now
chosen these components, we (1) rely that the variety of factor returned
is what we anticipate, and (2) that they comprise the textual content or different content material
that we anticipate.

The UI that we’re imagined to generate seems like this:

Take a look at-Driving HTML Templates

There are a number of particulars which are rendered dynamically:

  1. The variety of gadgets and their textual content content material change, clearly
  2. The type of the todo-item adjustments when it is accomplished (e.g., the
    second)
  3. The “2 gadgets left” textual content will change with the variety of non-completed
    gadgets
  4. One of many three buttons “All”, “Energetic”, “Accomplished” can be
    highlighted, relying on the present url; as an illustration if we determine that the
    url that exhibits solely the “Energetic” gadgets is /lively, then when the present url
    is /lively, the “Energetic” button ought to be surrounded by a skinny purple
    rectangle
  5. The “Clear accomplished” button ought to solely be seen if any merchandise is
    accomplished

Every of this considerations may be examined with the assistance of CSS selectors.

It is a snippet from the TodoMVC template (barely simplified). I
haven’t but added the dynamic bits, so what we see right here is static
content material, offered for instance:

index.tmpl

  <part class="todoapp">     <ul class="todo-list">       <!-- These are right here simply to point out the construction of the listing gadgets -->       <!-- Listing gadgets ought to get the category `accomplished` when marked as accomplished -->       <li class="accomplished">           <div class="view">           <enter class="toggle" kind="checkbox" checked>           <label>Style JavaScript</label>            <button class="destroy"></button>         </div>       </li>       <li>         <div class="view">           <enter class="toggle" kind="checkbox">           <label>Purchase a unicorn</label>            <button class="destroy"></button>         </div>       </li>     </ul>     <footer class="footer">       <!-- This ought to be `0 gadgets left` by default -->       <span class="todo-count"><sturdy>0</sturdy> merchandise left</span>        <ul class="filters">         <li>           <a class="chosen" href="#/">All</a>          </li>         <li>           <a href="#/lively">Energetic</a>         </li>         <li>           <a href="#/accomplished">Accomplished</a>         </li>       </ul>       <!-- Hidden if no accomplished gadgets are left ↓ -->       <button class="clear-completed">Clear accomplished</button>      </footer>   </part>  

supply

By trying on the static model of the template, we are able to deduce which
CSS selectors can be utilized to establish the related components for the 5 dynamic
options listed above:

characteristic CSS selector
All of the gadgets ul.todo-list li
Accomplished gadgets ul.todo-list li.accomplished
Objects left span.todo-count
Highlighted navigation hyperlink ul.filters a.chosen
Clear accomplished button button.clear-completed

We are able to use these selectors to focus our checks on simply the issues we wish to take a look at.

Testing HTML content material

The primary take a look at will search for all of the gadgets, and show that the information
arrange by the take a look at is rendered accurately.

func Test_todoItemsAreShown(t *testing.T) {   mannequin := todo.NewList()   mannequin.Add("Foo")   mannequin.Add("Bar")   buf := renderTemplate(mannequin)   // assert there are two <li> components contained in the <ul class="todo-list">    // assert the primary <li> textual content is "Foo"   // assert the second <li> textual content is "Bar" } 

We want a approach to question the HTML doc with our CSS selector; a very good
library for Go is goquery, that implements an API impressed by jQuery.
In Java, we preserve utilizing the identical library we used to check for sound HTML, specifically
jsoup. Our take a look at turns into:

Go

  func Test_todoItemsAreShown(t *testing.T) {     mannequin := todo.NewList()     mannequin.Add("Foo")     mannequin.Add("Bar")        buf := renderTemplate("index.tmpl", mannequin)        // parse the HTML with goquery     doc, err := goquery.NewDocumentFromReader(bytes.NewReader(buf.Bytes()))     if err != nil {       // if parsing fails, we cease the take a look at right here with t.FatalF       t.Fatalf("Error rendering template %s", err)     }        // assert there are two <li> components contained in the <ul class="todo-list">     choice := doc.Discover("ul.todo-list li")     assert.Equal(t, 2, choice.Size())        // assert the primary <li> textual content is "Foo"     assert.Equal(t, "Foo", textual content(choice.Nodes[0]))        // assert the second <li> textual content is "Bar"     assert.Equal(t, "Bar", textual content(choice.Nodes[1]))   }      func textual content(node *html.Node) string {     // A bit of mess on account of the truth that goquery has     // a .Textual content() technique on Choice however not on html.Node     sel := goquery.Choice{Nodes: []*html.Node{node}}     return strings.TrimSpace(sel.Textual content())   }

supply

Java

  @Take a look at   void todoItemsAreShown() throws IOException {       var mannequin = new TodoList();       mannequin.add("Foo");       mannequin.add("Bar");          var html = renderTemplate("/index.tmpl", mannequin);          // parse the HTML with jsoup       Doc doc = Jsoup.parse(html, "");          // assert there are two <li> components contained in the <ul class="todo-list">       var choice = doc.choose("ul.todo-list li");       assertThat(choice).hasSize(2);          // assert the primary <li> textual content is "Foo"       assertThat(choice.get(0).textual content()).isEqualTo("Foo");          // assert the second <li> textual content is "Bar"       assertThat(choice.get(1).textual content()).isEqualTo("Bar");   }

supply

If we nonetheless have not modified the template to populate the listing from the
mannequin, this take a look at will fail, as a result of the static template
todo gadgets have completely different textual content:

Go

  --- FAIL: Test_todoItemsAreShown (0.00s)       index_template_test.go:44: First listing merchandise: need Foo, received Style JavaScript       index_template_test.go:49: Second listing merchandise: need Bar, received Purchase a unicorn

Java

  IndexTemplateTest > todoItemsAreShown() FAILED       org.opentest4j.AssertionFailedError:       Anticipating:        <"Style JavaScript">       to be equal to:        <"Foo">       however was not.

We repair it by making the template use the mannequin knowledge:

Go

  <ul class="todo-list">     {{ vary .Objects }}       <li>         <div class="view">           <enter class="toggle" kind="checkbox">           <label>{{ .Title }}</label>           <button class="destroy"></button>         </div>       </li>     {{ finish }}   </ul>

supply

Java – jmustache

  <ul class="todo-list">     {{ #allItems }}     <li>       <div class="view">         <enter class="toggle" kind="checkbox">         <label>{{ title }}</label>         <button class="destroy"></button>       </div>     </li>     {{ /allItems }}   </ul>

supply

Take a look at each content material and soundness on the similar time

Our take a look at works, however it’s a bit verbose, particularly the Go model. If we will have extra
checks, they’ll change into repetitive and troublesome to learn, so we make it extra concise by extracting a helper perform for parsing the html. We additionally take away the
feedback, because the code ought to be clear sufficient

Go

  func Test_todoItemsAreShown(t *testing.T) {     mannequin := todo.NewList()     mannequin.Add("Foo")     mannequin.Add("Bar")        buf := renderTemplate("index.tmpl", mannequin)        doc := parseHtml(t, buf)     choice := doc.Discover("ul.todo-list li")     assert.Equal(t, 2, choice.Size())     assert.Equal(t, "Foo", textual content(choice.Nodes[0]))     assert.Equal(t, "Bar", textual content(choice.Nodes[1]))   }      func parseHtml(t *testing.T, buf bytes.Buffer) *goquery.Doc {     doc, err := goquery.NewDocumentFromReader(bytes.NewReader(buf.Bytes()))     if err != nil {       // if parsing fails, we cease the take a look at right here with t.FatalF       t.Fatalf("Error rendering template %s", err)     }     return doc   } 

Java

  @Take a look at   void todoItemsAreShown() throws IOException {       var mannequin = new TodoList();       mannequin.add("Foo");       mannequin.add("Bar");          var html = renderTemplate("/index.tmpl", mannequin);          var doc = parseHtml(html);       var choice = doc.choose("ul.todo-list li");       assertThat(choice).hasSize(2);       assertThat(choice.get(0).textual content()).isEqualTo("Foo");       assertThat(choice.get(1).textual content()).isEqualTo("Bar");   }      personal static Doc parseHtml(String html) {       return Jsoup.parse(html, "");   } 

Significantly better! No less than in my view. Now that we extracted the parseHtml helper, it is
a good suggestion to verify for sound HTML within the helper:

Go

  func parseHtml(t *testing.T, buf bytes.Buffer) *goquery.Doc {     assertWellFormedHtml(t, buf)     doc, err := goquery.NewDocumentFromReader(bytes.NewReader(buf.Bytes()))     if err != nil {       // if parsing fails, we cease the take a look at right here with t.FatalF       t.Fatalf("Error rendering template %s", err)     }     return doc   }

supply

Java

  personal static Doc parseHtml(String html) {       var parser = Parser.htmlParser().setTrackErrors(10);       var doc = Jsoup.parse(html, "", parser);       assertThat(parser.getErrors()).isEmpty();       return doc;   }

supply

And with this, we are able to do away with the primary take a look at that we wrote, as we are actually testing for sound HTML on a regular basis.

The second take a look at

Now we’re in a very good place for testing extra rendering logic. The
second dynamic characteristic in our listing is “Listing gadgets ought to get the category
accomplished when marked as accomplished”. We are able to write a take a look at for this:

Go

  func Test_completedItemsGetCompletedClass(t *testing.T) {     mannequin := todo.NewList()     mannequin.Add("Foo")     mannequin.AddCompleted("Bar")        buf := renderTemplate("index.tmpl", mannequin)        doc := parseHtml(t, buf)     choice := doc.Discover("ul.todo-list li.accomplished")     assert.Equal(t, 1, choice.Measurement())     assert.Equal(t, "Bar", textual content(choice.Nodes[0]))   }

supply

Java

  @Take a look at   void completedItemsGetCompletedClass() {       var mannequin = new TodoList();       mannequin.add("Foo");       mannequin.addCompleted("Bar");          var html = renderTemplate("/index.tmpl", mannequin);          Doc doc = Jsoup.parse(html, "");       var choice = doc.choose("ul.todo-list li.accomplished");       assertThat(choice).hasSize(1);       assertThat(choice.textual content()).isEqualTo("Bar");   }

supply

And this take a look at may be made inexperienced by including this little bit of logic to the
template:

Go

  <ul class="todo-list">     {{ vary .Objects }}       <li class="{{ if .IsCompleted }}accomplished{{ finish }}">         <div class="view">           <enter class="toggle" kind="checkbox">           <label>{{ .Title }}</label>           <button class="destroy"></button>         </div>       </li>     {{ finish }}   </ul>

supply

Java – jmustache

  <ul class="todo-list">     {{ #allItems }}     <li class="{{ #isCompleted }}accomplished{{ /isCompleted }}">       <div class="view">         <enter class="toggle" kind="checkbox">         <label>{{ title }}</label>         <button class="destroy"></button>       </div>     </li>     {{ /allItems }}   </ul>

supply

So little by little, we are able to take a look at and add the varied dynamic options
that our template ought to have.

Make it simple so as to add new checks

The primary of the 20 ideas from the wonderful discuss by Russ Cox on Go
Testing
is “Make it simple so as to add new take a look at instances“. Certainly, in Go there
is a bent to make most checks parameterized, for this very motive.
Then again, whereas Java has
good assist
for parameterized checks
with JUnit 5, they are not used as a lot.

Since our present two checks have the identical construction, we
may issue them right into a single parameterized take a look at.

A take a look at case for us will encompass:

  • A reputation (in order that we are able to produce clear error messages when the take a look at
    fails)
  • A mannequin (in our case a todo.Listing)
  • A CSS selector
  • A listing of textual content matches that we look forward to finding after we run the CSS
    selector on the rendered HTML.

So that is the information construction for our take a look at instances:

Go

  var testCases = []struct {     identify     string     mannequin    *todo.Listing     selector string     matches  []string   }{     {       identify: "all todo gadgets are proven",       mannequin: todo.NewList().         Add("Foo").         Add("Bar"),       selector: "ul.todo-list li",       matches:  []string{"Foo", "Bar"},     },     {       identify: "accomplished gadgets get the 'accomplished' class",       mannequin: todo.NewList().         Add("Foo").         AddCompleted("Bar"),       selector: "ul.todo-list li.accomplished",       matches:  []string{"Bar"},     },   }

supply

Java

  report TestCase(String identify,                   TodoList mannequin,                   String selector,                   Listing<String> matches) {       @Override       public String toString() {           return identify;       }   }      public static TestCase[] indexTestCases() {       return new TestCase[]{               new TestCase(                       "all todo gadgets are proven",                       new TodoList()                               .add("Foo")                               .add("Bar"),                       "ul.todo-list li",                       Listing.of("Foo", "Bar")),               new TestCase(                       "accomplished gadgets get the 'accomplished' class",                       new TodoList()                               .add("Foo")                               .addCompleted("Bar"),                       "ul.todo-list li.accomplished",                       Listing.of("Bar")),       };   }

supply

And that is our parameterized take a look at:

Go

  func Test_indexTemplate(t *testing.T) {     for _, take a look at := vary testCases {       t.Run(take a look at.identify, func(t *testing.T) {         buf := renderTemplate("index.tmpl", take a look at.mannequin)            assertWellFormedHtml(t, buf)         doc := parseHtml(t, buf)         choice := doc.Discover(take a look at.selector)         require.Equal(t, len(take a look at.matches), len(choice.Nodes), "surprising # of matches")         for i, node := vary choice.Nodes {           assert.Equal(t, take a look at.matches[i], textual content(node))         }       })     }   }

supply

Java

  @ParameterizedTest   @MethodSource("indexTestCases")   void testIndexTemplate(TestCase take a look at) {       var html = renderTemplate("/index.tmpl", take a look at.mannequin);          var doc = parseHtml(html);       var choice = doc.choose(take a look at.selector);       assertThat(choice).hasSize(take a look at.matches.measurement());       for (int i = 0; i < take a look at.matches.measurement(); i++) {           assertThat(choice.get(i).textual content()).isEqualTo(take a look at.matches.get(i));       }   }

supply

We are able to now run our parameterized take a look at and see it cross:

Go

  $ go take a look at -v   === RUN   Test_indexTemplate   === RUN   Test_indexTemplate/all_todo_items_are_shown   === RUN   Test_indexTemplate/completed_items_get_the_'accomplished'_class   --- PASS: Test_indexTemplate (0.00s)       --- PASS: Test_indexTemplate/all_todo_items_are_shown (0.00s)       --- PASS: Test_indexTemplate/completed_items_get_the_'accomplished'_class (0.00s)   PASS   okay    tdd-html-templates  0.608s

Java

  $ ./gradlew take a look at      > Activity :take a look at      IndexTemplateTest > testIndexTemplate(TestCase) > [1] all todo gadgets are proven PASSED   IndexTemplateTest > testIndexTemplate(TestCase) > [2] accomplished gadgets get the 'accomplished' class PASSED

Word how, by giving a reputation to our take a look at instances, we get very readable take a look at output, each on the terminal and within the IDE:

Having rewritten our two previous checks in desk kind, it is now tremendous simple so as to add
one other. That is the take a look at for the “x gadgets left” textual content:

Go

  {     identify: "gadgets left",     mannequin: todo.NewList().       Add("One").       Add("Two").       AddCompleted("Three"),     selector: "span.todo-count",     matches:  []string{"2 gadgets left"},   },

supply

Java

  new TestCase(       "gadgets left",       new TodoList()               .add("One")               .add("Two")               .addCompleted("Three"),       "span.todo-count",       Listing.of("2 gadgets left")),

supply

And the corresponding change within the html template is:

Go

  <span class="todo-count"><sturdy>{{len .ActiveItems}}</sturdy> gadgets left</span>

supply

Java – jmustache

  <span class="todo-count"><sturdy>{{activeItemsCount}}</sturdy> gadgets left</span>

supply

The above change within the template requires a supporting technique within the mannequin:

Go

  kind Merchandise struct {     Title       string     IsCompleted bool   }      kind Listing struct {     Objects []*Merchandise   }      func (l *Listing) ActiveItems() []*Merchandise {     var consequence []*Merchandise     for _, merchandise := vary l.Objects {       if !merchandise.IsCompleted {         consequence = append(consequence, merchandise)       }     }     return consequence   } 

supply

Java

  public class TodoList {       personal last Listing<TodoItem> gadgets = new ArrayList<>();       // ...       public lengthy activeItemsCount() {           return gadgets.stream().filter(TodoItem::isActive).rely();       }   }

supply

We have invested just a little effort in our testing infrastructure, in order that including new
take a look at instances is less complicated. Within the subsequent part, we’ll see that the necessities
for the following take a look at instances will push us to refine our take a look at infrastructure additional.

Making the desk extra expressive, on the expense of the take a look at code

We are going to now take a look at the “All”, “Energetic” and “Accomplished” navigation hyperlinks at
the underside of the UI (see the image above),
and these rely upon which url we’re visiting, which is
one thing that our template has no approach to discover out.

At present, all we cross to our template is our mannequin, which is a todo-list.
It is not appropriate so as to add the presently visited url to the mannequin, as a result of that’s
consumer navigation state, not utility state.

So we have to cross extra info to the template past the mannequin. A simple manner
is to cross a map, which we assemble in our
renderTemplate perform:

Go

  func renderTemplate(mannequin *todo.Listing, path string) bytes.Buffer {     templ := template.Should(template.ParseFiles("index.tmpl"))     var buf bytes.Buffer     knowledge := map[string]any{       "mannequin": mannequin,       "path":  path,     }     err := templ.Execute(&buf, knowledge)     if err != nil {       panic(err)     }     return buf   }

Java

  personal String renderTemplate(String templateName, TodoList mannequin, String path) {       var template = Mustache.compiler().compile(               new InputStreamReader(                       getClass().getResourceAsStream(templateName)));       var knowledge = Map.of(               "mannequin", mannequin,               "path", path       );       return template.execute(knowledge);   }

And correspondingly our take a look at instances desk has yet one more discipline:

Go

  var testCases = []struct {     identify     string     mannequin    *todo.Listing     path     string     selector string     matches  []string   }{     {       identify: "all todo gadgets are proven",       mannequin: todo.NewList().         Add("Foo").         Add("Bar"),       selector: "ul.todo-list li",       matches:  []string{"Foo", "Bar"},     },   // ... the opposite instances     {       identify:     "highlighted navigation hyperlink: All",       path:     "/",       selector: "ul.filters a.chosen",       matches:  []string{"All"},     },     {       identify:     "highlighted navigation hyperlink: Energetic",       path:     "/lively",       selector: "ul.filters a.chosen",       matches:  []string{"Energetic"},     },     {       identify:     "highlighted navigation hyperlink: Accomplished",       path:     "/accomplished",       selector: "ul.filters a.chosen",       matches:  []string{"Accomplished"},     },   }

Java

  report TestCase(String identify,                   TodoList mannequin,                   String path,                   String selector,                   Listing<String> matches) {       @Override       public String toString() {           return identify;       }   }      public static TestCase[] indexTestCases() {       return new TestCase[]{               new TestCase(                       "all todo gadgets are proven",                       new TodoList()                               .add("Foo")                               .add("Bar"),                       "/",                       "ul.todo-list li",                       Listing.of("Foo", "Bar")),               // ... the earlier instances               new TestCase(                       "highlighted navigation hyperlink: All",                       new TodoList(),                       "/",                       "ul.filters a.chosen",                       Listing.of("All")),               new TestCase(                       "highlighted navigation hyperlink: Energetic",                       new TodoList(),                       "/lively",                       "ul.filters a.chosen",                       Listing.of("Energetic")),               new TestCase(                       "highlighted navigation hyperlink: Accomplished",                       new TodoList(),                       "/accomplished",                       "ul.filters a.chosen",                       Listing.of("Accomplished")),       };   }

We discover that for the three new instances, the mannequin is irrelevant;
whereas for the earlier instances, the trail is irrelevant. The Go syntax permits us
to initialize a struct with simply the fields we’re serious about, however Java doesn’t have
the same characteristic, so we’re pushed to cross additional info, and this makes the take a look at instances
desk more durable to grasp.

A developer may take a look at the primary take a look at case and surprise if the anticipated conduct relies upon
on the trail being set to "/", and is likely to be tempted so as to add extra instances with
a distinct path. In the identical manner, when studying the
highlighted navigation hyperlink take a look at instances, the developer may surprise if the
anticipated conduct is dependent upon the mannequin being set to an empty todo listing. If that’s the case, one may
be led so as to add irrelevant take a look at instances for the highlighted hyperlink with non-empty todo-lists.

We wish to optimize for the time of the builders, so it is worthwhile to keep away from including irrelevant
knowledge to our take a look at case. In Java we would cross null for the
irrelevant fields, however there’s a greater manner: we are able to use
the builder sample,
popularized by Joshua Bloch.
We are able to shortly write one for the Java TestCase report this manner:

Java

  report TestCase(String identify,                   TodoList mannequin,                   String path,                   String selector,                   Listing<String> matches) {       @Override       public String toString() {           return identify;       }          public static last class Builder {           String identify;           TodoList mannequin;           String path;           String selector;           Listing<String> matches;              public Builder identify(String identify) {               this.identify = identify;               return this;           }              public Builder mannequin(TodoList mannequin) {               this.mannequin = mannequin;               return this;           }              public Builder path(String path) {               this.path = path;               return this;           }              public Builder selector(String selector) {               this.selector = selector;               return this;           }              public Builder matches(String ... matches) {               this.matches = Arrays.asList(matches);               return this;           }              public TestCase construct() {               return new TestCase(identify, mannequin, path, selector, matches);           }       }   }

Hand-coding builders is just a little tedious, however doable, although there are
automated methods to put in writing them.
Now we are able to rewrite our Java take a look at instances with the Builder, to
obtain better readability:

Java

  public static TestCase[] indexTestCases() {       return new TestCase[]{               new TestCase.Builder()                       .identify("all todo gadgets are proven")                       .mannequin(new TodoList()                               .add("Foo")                               .add("Bar"))                       .selector("ul.todo-list li")                       .matches("Foo", "Bar")                       .construct(),               // ... different instances               new TestCase.Builder()                       .identify("highlighted navigation hyperlink: Accomplished")                       .path("/accomplished")                       .selector("ul.filters a.chosen")                       .matches("Accomplished")                       .construct(),       };   }

So, the place are we with our checks? At current, they fail for the improper motive: null-pointer exceptions
as a result of lacking mannequin and path values.
In an effort to get our new take a look at instances to fail for the precise motive, specifically that the template does
not but have logic to spotlight the right hyperlink, we should
present default values for mannequin and path. In Go, we are able to do that
within the take a look at technique:

Go

  func Test_indexTemplate(t *testing.T) {     for _, take a look at := vary testCases {       t.Run(take a look at.identify, func(t *testing.T) {         if take a look at.mannequin == nil {           take a look at.mannequin = todo.NewList()         }         buf := renderTemplate(take a look at.mannequin, take a look at.path)         // ... similar as earlier than        })     }   }

supply

In Java, we are able to present default values within the builder:

Java

  public static last class Builder {       String identify;       TodoList mannequin = new TodoList();       String path = "/";       String selector;       Listing<String> matches;       // ...   }

supply

With these adjustments, we see that the final two take a look at instances, those for the highlighted hyperlink Energetic
and Accomplished fail, for the anticipated motive that the highlighted hyperlink doesn’t change:

Go

  === RUN   Test_indexTemplate/highlighted_navigation_link:_Active       index_template_test.go:82:              Error Hint:  .../tdd-templates/go/index_template_test.go:82             Error:        Not equal:                            anticipated: "Energetic"                           precise  : "All"   === RUN   Test_indexTemplate/highlighted_navigation_link:_Completed       index_template_test.go:82:              Error Hint:  .../tdd-templates/go/index_template_test.go:82             Error:        Not equal:                            anticipated: "Accomplished"                           precise  : "All" 

Java

  IndexTemplateTest > testIndexTemplate(TestCase) > [5] highlighted navigation hyperlink: Energetic FAILED       org.opentest4j.AssertionFailedError:       Anticipating:        <"All">       to be equal to:        <"Energetic">       however was not.      IndexTemplateTest > testIndexTemplate(TestCase) > [6] highlighted navigation hyperlink: Accomplished FAILED       org.opentest4j.AssertionFailedError:       Anticipating:        <"All">       to be equal to:        <"Accomplished">       however was not.

To make the checks cross, we make these adjustments to the template:

Go

  <ul class="filters">     <li>       <a class="{{ if eq .path "/" }}chosen{{ finish }}" href="#/">All</a>     </li>     <li>       <a class="{{ if eq .path "/lively" }}chosen{{ finish }}" href="#/lively">Energetic</a>     </li>     <li>       <a class="{{ if eq .path "/accomplished" }}chosen{{ finish }}" href="#/accomplished">Accomplished</a>     </li>   </ul>

supply

Java – jmustache

  <ul class="filters">     <li>       <a class="{{ #pathRoot }}chosen{{ /pathRoot }}" href="#/">All</a>     </li>     <li>       <a class="{{ #pathActive }}chosen{{ /pathActive }}" href="#/lively">Energetic</a>     </li>     <li>       <a class="{{ #pathCompleted }}chosen{{ /pathCompleted }}" href="#/accomplished">Accomplished</a>     </li>   </ul>

supply

Because the Mustache template language doesn’t permit for equality testing, we should change the
knowledge handed to the template in order that we execute the equality checks earlier than rendering the template:

Java

  personal String renderTemplate(String templateName, TodoList mannequin, String path) {       var template = Mustache.compiler().compile(               new InputStreamReader(                       getClass().getResourceAsStream(templateName)));       var knowledge = Map.of(               "mannequin", mannequin,               "pathRoot", path.equals("/"),               "pathActive", path.equals("/lively"),               "pathCompleted", path.equals("/accomplished")       );       return template.execute(knowledge);   }

supply

And with these adjustments, all of our checks now cross.

To recap this part, we made the take a look at code just a little bit extra difficult, in order that the take a look at
instances are clearer: it is a superb tradeoff!

Stage 3: testing HTML behaviour

Within the story up to now, we examined the behaviour of the HTML
templates
, by checking the construction of the generated HTML.
That is good, however what if we needed to check the behaviour of the HTML
itself, plus any CSS and JavaScript it might use?

The behaviour of HTML by itself is normally fairly apparent, as a result of
there’s not a lot of it. The one components that may work together with the
consumer are the anchor (<a>), <kind> and
<enter> components, however the image adjustments fully when
we add CSS, that may cover, present, transfer round issues and plenty extra, and
with JavaScript, that may add any behaviour to a web page.

In an utility that’s primarily rendered server-side, we anticipate
that almost all behaviour is applied by returning new HTML with a
round-trip to the consumer, and this may be examined adequately with the
strategies we have seen up to now, however what if we needed to hurry up the
utility behaviour with a library similar to HTMX? This library works by particular
attributes which are added to components so as to add Ajax behaviour. These
attributes are in impact a DSL that we would wish to
take a look at.

How can we take a look at the mixture of HTML, CSS and JavaScript in
a unit take a look at?

Testing HTML, CSS and JavaScript requires one thing that is ready to
interpret and execute their behaviours; in different phrases, we want a
browser! It’s customary to make use of headless browsers in end-to-end checks;
can we use them for unitary checks as a substitute? I believe that is potential,
utilizing the next strategies, though I have to admit I’ve but to strive
this on an actual challenge.

We are going to use the Playwright
library, that’s accessible for each Go and
Java. The checks we
are going to put in writing can be slower, as a result of we must wait a number of
seconds for the headless browser to begin, however will retain a few of the
vital traits of unit checks, primarily that we’re testing
simply the HTML (and any related CSS and JavaScript), in isolation from
some other server-side logic.

Persevering with with the TodoMVC
instance, the following factor we would wish to take a look at is what occurs when the
consumer clicks on the checkbox of a todo merchandise. What we might wish to occur is
that:

  1. A POST name to the server is made, in order that the appliance is aware of
    that the state of a todo merchandise has modified
  2. The server returns new HTML for the dynamic a part of the web page,
    specifically the entire part with class “todoapp”, in order that we are able to present the
    new state of the appliance together with the rely of remaining “lively”
    gadgets (see the template above)
  3. The web page replaces the previous contents of the “todoapp” part with
    the brand new ones.

Loading the web page within the Playwright browser

We begin with a take a look at that can simply load the preliminary HTML. The take a look at
is just a little concerned, so I present the whole code right here, after which I’ll
remark it little by little.

Go

  func Test_toggleTodoItem(t *testing.T) {     // render the preliminary HTML     mannequin := todo.NewList().       Add("One").       Add("Two")     initialHtml := renderTemplate("index.tmpl", mannequin, "/")        // open the browser web page with Playwright     web page := openPage()     defer web page.Shut()     logActivity(web page)        // stub community calls     err := web page.Route("**", func(route playwright.Route) {       if route.Request().URL() == "http://localhost:4567/index.html" {         // serve the preliminary HTML         stubResponse(route, initialHtml.String(), "textual content/html")       } else {         // keep away from surprising requests         panic("surprising request: " + route.Request().URL())       }     })     if err != nil {       t.Deadly(err)     }        // load preliminary HTML within the web page     response, err := web page.Goto("http://localhost:4567/index.html")     if err != nil {       t.Deadly(err)     }     if response.Standing() != 200 {       t.Fatalf("surprising standing: %d", response.Standing())     }   }

supply

Java

  public class IndexBehaviourTest {       static Playwright playwright;       static Browser browser;          @BeforeAll       static void launchBrowser() {           playwright = Playwright.create();           browser = playwright.chromium().launch();       }          @AfterAll       static void closeBrowser() {           playwright.shut();       }          @Take a look at       void toggleTodoItem() {           // Render the preliminary html           TodoList mannequin = new TodoList()                   .add("One")                   .add("Two");           String initialHtml = renderTemplate("/index.tmpl", mannequin, "/");                      strive (Web page web page = browser.newPage()) {               logActivity(web page);                  // stub community calls               web page.route("**", route -> {                   if (route.request().url().equals("http://localhost:4567/index.html")) {                       // serve the preliminary HTML                       route.fulfill(new Route.FulfillOptions()                               .setContentType("textual content/html")                               .setBody(initialHtml));                   } else {                       // we do not need surprising calls                       fail(String.format("Sudden request: %s %s", route.request().technique(), route.request().url()));                   }               });                          // load preliminary html               web page.navigate("http://localhost:4567/index.html");           }       }   }

supply

At first of the take a look at, we initialize the mannequin with two todo
gadgets “One” and “Two”, then we render the template as earlier than:

Go

  mannequin := todo.NewList().     Add("One").     Add("Two")   initialHtml := renderTemplate("index.tmpl", mannequin, "/") 

Java

  TodoList mannequin = new TodoList()           .add("One")           .add("Two");   String initialHtml = renderTemplate("/index.tmpl", mannequin, "/");

Then we open the Playwright “web page”, which is able to begin a headless
browser

Go

  web page := openPage()   defer web page.Shut()   logActivity(web page) 

Java

  strive (Web page web page = browser.newPage()) {       logActivity(web page);

The openPage perform in Go returns a Playwright
Web page object,

Go

  func openPage() playwright.Web page {     pw, err := playwright.Run()     if err != nil {       log.Fatalf("couldn't begin playwright: %v", err)     }     browser, err := pw.Chromium.Launch()     if err != nil {       log.Fatalf("couldn't launch browser: %v", err)     }     web page, err := browser.NewPage()     if err != nil {       log.Fatalf("couldn't create web page: %v", err)     }     return web page   }

and the logActivity perform gives suggestions on what
the web page is doing

Go

  func logActivity(web page playwright.Web page) {     web page.OnRequest(func(request playwright.Request) {       log.Printf(">> %s %sn", request.Technique(), request.URL())     })     web page.OnResponse(func(response playwright.Response) {       log.Printf("<< %d %sn", response.Standing(), response.URL())     })     web page.OnLoad(func(web page playwright.Web page) {       log.Println("Loaded: " + web page.URL())     })     web page.OnConsole(func(message playwright.ConsoleMessage) {       log.Println("!  " + message.Textual content())     })   }

Java

  personal void logActivity(Web page web page) {       web page.onRequest(request -> System.out.printf(">> %s %spercentn", request.technique(), request.url()));       web page.onResponse(response -> System.out.printf("<< %s %spercentn", response.standing(), response.url()));       web page.onLoad(page1 -> System.out.println("Loaded: " + page1.url()));       web page.onConsoleMessage(consoleMessage -> System.out.println("!  " + consoleMessage.textual content()));   }

Then we stub all community exercise that the web page may attempt to do

Go

  err := web page.Route("**", func(route playwright.Route) {     if route.Request().URL() == "http://localhost:4567/index.html" {       // serve the preliminary HTML       stubResponse(route, initialHtml.String(), "textual content/html")     } else {       // keep away from surprising requests       panic("surprising request: " + route.Request().URL())     }   }) 

Java

  // stub community calls   web page.route("**", route -> {       if (route.request().url().equals("http://localhost:4567/index.html")) {           // serve the preliminary HTML           route.fulfill(new Route.FulfillOptions()                   .setContentType("textual content/html")                   .setBody(initialHtml));       } else {           // we do not need surprising calls           fail(String.format("Sudden request: %s %s", route.request().technique(), route.request().url()));       }   });

and we ask the web page to load the preliminary HTML

Go

  response, err := web page.Goto("http://localhost:4567/index.html") 

Java

  web page.navigate("http://localhost:4567/index.html");

With all this equipment in place, we run the take a look at; it succeeds and
it logs the stubbed community exercise on normal output:

Go

  === RUN   Test_toggleTodoItem   >> GET http://localhost:4567/index.html   << 200 http://localhost:4567/index.html   Loaded: http://localhost:4567/index.html   --- PASS: Test_toggleTodoItem (0.89s)

Java

  IndexBehaviourTest > toggleTodoItem() STANDARD_OUT       >> GET http://localhost:4567/index.html       << 200 http://localhost:4567/index.html       Loaded: http://localhost:4567/index.html      IndexBehaviourTest > toggleTodoItem() PASSED

So with this take a look at we are actually capable of load arbitrary HTML in a
headless browser. Within the subsequent sections we’ll see methods to simulate consumer
interplay with components of the web page, and observe the web page’s
behaviour. However first we have to remedy an issue with the dearth of
identifiers in our area mannequin.

Figuring out todo gadgets

Now we wish to click on on the “One” checkbox. The issue we have now is
that at current, we have now no approach to establish particular person todo gadgets, so
we introduce an Id discipline within the todo merchandise:

Go – up to date mannequin with Id

  kind Merchandise struct {     Id          int     Title       string     IsCompleted bool   }      func (l *Listing) AddWithId(id int, title string) *Listing {     merchandise := Merchandise{       Id:    id,       Title: title,     }     l.Objects = append(l.Objects, &merchandise)     return l   }      // Add creates a brand new todo.Merchandise with a random Id   func (l *Listing) Add(title string) *Listing {     merchandise := Merchandise{       Id:    generateRandomId(),       Title: title,     }     l.Objects = append(l.Objects, &merchandise)     return l   }      func generateRandomId() int {     return abs(rand.Int())   }

Java – up to date mannequin with Id

  public class TodoList {       personal last Listing<TodoItem> gadgets = new ArrayList<>();          public TodoList add(String title) {           gadgets.add(new TodoItem(generateRandomId(), title, false));           return this;       }          public TodoList addCompleted(String title) {           gadgets.add(new TodoItem(generateRandomId(), title, true));           return this;       }          public TodoList add(int id, String title) {           gadgets.add(new TodoItem(id, title, false));           return this;       }          personal static int generateRandomId() {           return new Random().nextInt(0, Integer.MAX_VALUE);       }   }      public report TodoItem(int id, String title, boolean isCompleted) {       public boolean isActive() {           return !isCompleted;       }   }

And we replace the mannequin in our take a look at so as to add specific Ids

Go – including Id within the take a look at knowledge

  func Test_toggleTodoItem(t *testing.T) {     // render the preliminary HTML     mannequin := todo.NewList().       AddWithId(101, "One").       AddWithId(102, "Two")     initialHtml := renderTemplate("index.tmpl", mannequin, "/")     // ...    }

Java – including Id within the take a look at knowledge

  @Take a look at   void toggleTodoItem() {       // Render the preliminary html       TodoList mannequin = new TodoList()               .add(101, "One")               .add(102, "Two");       String initialHtml = renderTemplate("/index.tmpl", mannequin, "/");   }

We are actually prepared to check consumer interplay with the web page.

Clicking on a todo merchandise

We wish to simulate consumer interplay with the HTML web page. It is likely to be
tempting to proceed to make use of CSS selectors to establish the particular
checkbox that we wish to click on, however there’s a greater manner: there’s a
consensus amongst front-end builders that one of the simplest ways to check
interplay with a web page is to make use of it
the identical manner that customers do
. As an illustration, you do not search for a
button by a CSS locator similar to button.purchase; as a substitute,
you search for one thing clickable with the label “Purchase”. In apply,
this implies figuring out components of the web page by their
ARIA
roles.

To this finish, we add code to our take a look at to search for a checkbox labelled
“One”:

Go

  func Test_toggleTodoItem(t *testing.T) {     // ...     // click on on the "One" checkbox     checkbox := web page.GetByRole(*playwright.AriaRoleCheckbox, playwright.PageGetByRoleOptions{Title: "One"})     if err := checkbox.Click on(); err != nil {       t.Deadly(err)     }   }

Java

  @Take a look at   void toggleTodoItem() {           // ...           // click on on the "One" checkbox           var checkbox = web page.getByRole(AriaRole.CHECKBOX, new Web page.GetByRoleOptions().setName("One"));           checkbox.click on();       }   }

We run the take a look at, and it fails:

Go

  >> GET http://localhost:4567/index.html   << 200 http://localhost:4567/index.html   Loaded: http://localhost:4567/index.html   --- FAIL: Test_toggleTodoItem (32.74s)       index_behaviour_test.go:50: playwright: timeout: Timeout 30000ms exceeded.

Java

  IndexBehaviourTest > toggleTodoItem() STANDARD_OUT       >> GET http://localhost:4567/index.html       << 200 http://localhost:4567/index.html       Loaded: http://localhost:4567/index.html      IndexBehaviourTest > toggleTodoItem() FAILED       com.microsoft.playwright.TimeoutError: Error {         message="hyperlink the label to the checkbox correctly: 

generated HTML with unhealthy accessibility

  <li>     <div class="view">       <enter class="toggle" kind="checkbox">       <label>One</label>       <button class="destroy"></button>     </div>   </li>

We repair it by utilizing the for attribute within the
template,

index.tmpl – Go

  <li>     <div class="view">       <enter id="checkbox-{{.Id}}" class="toggle" kind="checkbox">       <label for="checkbox-{{.Id}}">{{.Title}}</label>       <button class="destroy"></button>     </div>   </li>

index.tmpl – Java

  <li>     <div class="view">       <enter id="checkbox-{{ id }}" class="toggle" kind="checkbox">       <label for="checkbox-{{ id }}">{{ title }}</label>       <button class="destroy"></button>     </div>   </li>

In order that it generates correct, accessible HTML:

generated HTML with higher accessibility

  <li>     <div class="view">       <enter id="checkbox-101" class="toggle" kind="checkbox">       <label for="checkbox-101">One</label>       <button class="destroy"></button>     </div>   </li>

We run once more the take a look at, and it passes.

On this part we noticed how testing the HTML in the identical was as customers
work together with it led us to make use of ARIA roles, which led to enhancing
accessibility of our generated HTML. Within the subsequent part, we are going to see
methods to take a look at that the clicking on a todo merchandise triggers a distant name to the
server, that ought to lead to swapping part of the present HTML with
the HTML returned by the XHR name.

Spherical-trip to the server

Now we are going to lengthen our take a look at. We inform the take a look at that if name to
POST /toggle/101 is obtained, it ought to return some
stubbed HTML.

Go

  } else if route.Request().URL() == "http://localhost:4567/toggle/101" && route.Request().Technique() == "POST" {     // we anticipate {that a} POST /toggle/101 request is made after we click on on the "One" checkbox     const stubbedHtml = `       <part class="todoapp">         <p>Stubbed html</p>       </part>`     stubResponse(route, stubbedHtml, "textual content/html")

Java

  } else if (route.request().url().equals("http://localhost:4567/toggle/101") && route.request().technique().equals("POST")) {       // we anticipate {that a} POST /toggle/101 request is made after we click on on the "One" checkbox       String stubbedHtml = """           <part class="todoapp">               <p>Stubbed html</p>           </part>           """;       route.fulfill(new Route.FulfillOptions()               .setContentType("textual content/html")               .setBody(stubbedHtml));

And we stub the loading of the HTMX library, which we load from a
native file:

Go

  } else if route.Request().URL() == "https://unpkg.com/htmx.org@1.9.12" {     // serve the htmx library     stubResponse(route, readFile("testdata/htmx.min.js"), "utility/javascript")

Go

  } else if (route.request().url().equals("https://unpkg.com/htmx.org@1.9.12")) {       // serve the htmx library       route.fulfill(new Route.FulfillOptions()               .setContentType("textual content/html")               .setBody(readFile("/htmx.min.js")));

Lastly, we add the expectation that, after we click on the checkbox,
the part of the HTML that comprises a lot of the utility is
reloaded.

Go

  // click on on the "One" checkbox   checkbox := web page.GetByRole(*playwright.AriaRoleCheckbox, playwright.PageGetByRoleOptions{Title: "One"})   if err := checkbox.Click on(); err != nil {     t.Deadly(err)   }   // verify that the web page has been up to date   doc := parseHtml(t, content material(t, web page))   components := doc.Discover("physique > part.todoapp > p")   assert.Equal(t, "Stubbed html", components.Textual content(), should(web page.Content material())) 

java

  // click on on the "One" checkbox   var checkbox = web page.getByRole(AriaRole.CHECKBOX, new Web page.GetByRoleOptions().setName("One"));   checkbox.click on();   // verify that the web page has been up to date   var doc = parseHtml(web page.content material());   var components = doc.choose("physique > part.todoapp > p");   assertThat(components.textual content())           .describedAs(web page.content material())           .isEqualTo("Stubbed html");

We run the take a look at, and it fails, as anticipated. In an effort to perceive
why precisely it fails, we add to the error message the entire HTML
doc.

Go

  assert.Equal(t, "Stubbed html", components.Textual content(), should(web page.Content material())) 

Java

  assertThat(components.textual content())           .describedAs(web page.content material())           .isEqualTo("Stubbed html");

The error message may be very verbose, however we see that the rationale it
fails is that we do not see the stubbed HTML within the output. This implies
that the web page didn’t make the anticipated XHR name.

Go – Java is comparable

  --- FAIL: Test_toggleTodoItem (2.75s)   === RUN   Test_toggleTodoItem   >> GET http://localhost:4567/index.html   << 200 http://localhost:4567/index.html   Loaded: http://localhost:4567/index.html       index_behaviour_test.go:67:             Error Hint:  .../index_behaviour_test.go:67             Error:        Not equal:                           anticipated: "Stubbed html"                           precise  : ""                           ...             Take a look at:         Test_toggleTodoItem             Messages:     <!DOCTYPE html><html lang="en"><head>                               <meta charset="utf-8">                               <meta identify="viewport" content material="width=device-width, initial-scale=1">                               <title>Template • TodoMVC</title>                               <script src="https://unpkg.com/htmx.org@1.9.12"></script>                             <physique>                               <part class="todoapp">                           ...                                     <li class="">                                       <div class="view">                                         <enter id="checkbox-101" class="toggle" kind="checkbox">                                         <label for="checkbox-101">One</label>                                         <button class="destroy"></button>                                       </div>                                     </li>                           ...

We are able to make this take a look at cross by altering the HTML template to make use of HTMX
to make an XHR name again to the server. First we load the HTMX
library:

index.tmpl

  <title>Template • TodoMVC</title>   <script src="https://unpkg.com/htmx.org@1.9.12"></script> 

Then we add the HTMX attributes to the checkboxes:

index.tmpl

  <enter       data-hx-post="/toggle/{{.Id}}"       data-hx-target="part.todoapp"       id="checkbox-{{.Id}}"       class="toggle"       kind="checkbox">

The data-hx-post annotation will make HTMX do a POST
name to the required url. The data-hx-target tells HTMX
to repeat the HTML returned by the decision, to the factor specified by the
part.todoapp CSS locator.

We run once more the take a look at, and it nonetheless fails!

Go – Java is comparable

  --- FAIL: Test_toggleTodoItem (2.40s)   === RUN   Test_toggleTodoItem   >> GET http://localhost:4567/index.html   << 200 http://localhost:4567/index.html   >> GET https://unpkg.com/htmx.org@1.9.12   << 200 https://unpkg.com/htmx.org@1.9.12   Loaded: http://localhost:4567/index.html   >> POST http://localhost:4567/toggle/101   << 200 http://localhost:4567/toggle/101       index_behaviour_test.go:67:             Error Hint:  .../index_behaviour_test.go:67             Error:        Not equal:                           anticipated: "Stubbed html"                           precise  : ""                           ...             Take a look at:         Test_toggleTodoItem             Messages:     <!DOCTYPE html><html lang="en"><head>                               <meta charset="utf-8">                               <meta identify="viewport" content material="width=device-width, initial-scale=1">                               <title>Template • TodoMVC</title>                               <script src="https://unpkg.com/htmx.org@1.9.12"></script>                           ...                             <physique>                               <part class="todoapp"><part class="todoapp">                                     <p>Stubbed html</p>                                   </part></part>                           ...                           </physique></html>

The log strains present that the POST name occurred as anticipated, however
examination of the error message exhibits that the HTML construction we
anticipated will not be there: we have now a part.todoapp nested
inside one other. Which means that we’re not utilizing the HTMX annotations
accurately, and exhibits why this sort of take a look at may be useful. We add the
lacking annotation

index.tmpl

  <enter       data-hx-post="/toggle/{{.Id}}"       data-hx-target="part.todoapp"       data-hx-swap="outerHTML"       id="checkbox-{{.Id}}"       class="toggle"       kind="checkbox">

The default behaviour of HTMX is to switch the inside HTML of the
goal factor. The data-hx-swap="outerHTML" annotation
tells HTMX to switch the outer HTML as a substitute.

and we take a look at once more, and this time it passes!

Go

  === RUN   Test_toggleTodoItem   >> GET http://localhost:4567/index.html   << 200 http://localhost:4567/index.html   >> GET https://unpkg.com/htmx.org@1.9.12   << 200 https://unpkg.com/htmx.org@1.9.12   Loaded: http://localhost:4567/index.html   >> POST http://localhost:4567/toggle/101   << 200 http://localhost:4567/toggle/101   --- PASS: Test_toggleTodoItem (1.39s)

Java

  IndexBehaviourTest > toggleTodoItem() STANDARD_OUT       >> GET http://localhost:4567/index.html       << 200 http://localhost:4567/index.html       >> GET https://unpkg.com/htmx.org@1.9.12       << 200 https://unpkg.com/htmx.org@1.9.12       Loaded: http://localhost:4567/index.html       >> POST http://localhost:4567/toggle/101       << 200 http://localhost:4567/toggle/101      IndexBehaviourTest > toggleTodoItem() PASSED

On this part we noticed methods to write a take a look at for the behaviour of our
HTML that, whereas utilizing the difficult equipment of a headless browser,
nonetheless feels extra like a unit take a look at than an integration take a look at. It’s in
reality testing simply an HTML web page with any related CSS and JavaScript,
in isolation from different components of the appliance similar to controllers,
providers or repositories.

The take a look at prices 2-3 seconds of ready time for the headless browser to return up, which is normally an excessive amount of for a unit take a look at; nonetheless, like a unit take a look at, it is rather secure, as it isn’t flaky, and its failures are documented with a comparatively clear error message.

See the ultimate model of the take a look at in Go and in Java.

Bonus degree: Stringly asserted

Esko Luontola, TDD knowledgeable and creator of the net course tdd.mooc.fi, urged an alternate to testing HTML with CSS selectors: the concept is to rework HTML right into a human-readable canonical kind.

Let’s take for instance this snippet of generated HTML:

<ul class="todo-list">   <li class="">     <div class="view">       <enter id="checkbox-100" class="toggle" kind="checkbox">       <label for="checkbox-100">One</label>       <button class="destroy"></button>     </div>   </li>   <li class="">     <div class="view">       <enter id="checkbox-200" class="toggle" kind="checkbox">       <label for="checkbox-200">Two</label>       <button class="destroy"></button>     </div>   </li>   <li class="accomplished">     <div class="view">       <enter id="checkbox-300" class="toggle" kind="checkbox">       <label for="checkbox-300">Three</label>       <button class="destroy"></button>     </div>   </li> </ul> 

We may visualize the above HTML by:

  1. deleting all HTML tags
  2. lowering each sequence of whitespace characters to a single clean

to reach at:

One Two Three

This, nonetheless, removes an excessive amount of of the HTML construction to be helpful. As an illustration, it doesn’t allow us to distinguish between lively and accomplished gadgets. Some HTML factor signify seen content material: as an illustration

<enter worth="foo" />

exhibits a textual content field with the phrase “foo” that is a crucial a part of the manner we understand HTML. To visualise these components, Esko suggests so as to add a data-test-icon attribute that provides some textual content for use rather than the factor when visualizing it for testing. With this,

<enter worth="foo" data-test-icon="[foo]" />

the enter factor is visualized as [foo], with the sq. brackets hinting that the phrase “foo” sits inside an editable textual content field. Now if we add test-icons to our HTML template,

Go — Java is comparable

  <ul class="todo-list">       {{ vary .mannequin.AllItems }}       <li class="{{ if .IsCompleted }}accomplished{{ finish }}">           <div class="view">               <enter data-hx-post="/toggle/{{ .Id }}"                      data-hx-target="part.todoapp"                      data-hx-swap="outerHTML"                      id="checkbox-{{ .Id }}"                      class="toggle"                      kind="checkbox"                      data-test-icon="{{ if .IsCompleted }}✅{{ else }}⬜{{ finish }}">               <label for="checkbox-{{ .Id }}">{{ .Title }}</label>               <button class="destroy" data-test-icon="❌️"></button>           </div>       </li>       {{ finish }}   </ul>

we are able to assert towards its canonical visible illustration like this:

Go

  func Test_visualize_html_example(t *testing.T) {     mannequin := todo.NewList().       Add("One").       Add("Two").       AddCompleted("Three")        buf := renderTemplate("todo-list.tmpl", mannequin, "/")        anticipated := `       ⬜ One ❌️       ⬜ Two ❌️       ✅ Three ❌️       `     assert.Equal(t, normalizeWhitespace(anticipated), visualizeHtml(buf.String()))   }

Java

  @Take a look at   void visualize_html_example() {       var mannequin = new TodoList()               .add("One")               .add("Two")               .addCompleted("Three");          var html = renderTemplate("/todo-list.tmpl", mannequin, "/");          assertThat(visualizeHtml(html))               .isEqualTo(normalizeWhitespace("""                       ⬜ One ❌️                       ⬜ Two ❌️                       ✅ Three ❌️                       """));   }

Right here is Esko Luontola’s Java implementation of the 2 capabilities that make this potential, and my translation to Go of his code.

Go

  func visualizeHtml(html string) string sturdy      func normalizeWhitespace(s string) string {     return strings.TrimSpace(replaceAll(s, "s+", " "))   }      func replaceAll(src, regex, repl string) string {     re := regexp.MustCompile(regex)     return re.ReplaceAllString(src, repl)   }

supply

Java

  public static String visualizeHtml(String html) i      public static String normalizeWhitespace(String s) {      return s.replaceAll("s+", " ").trim();   }

supply

On this part, we have now seen a way for asserting HTML content material that’s an alternative choice to the CSS selector-based method utilized in the remainder of the article. Esko Luontola has reported nice success with it, and I hope readers have success with it too!

This system of asserting towards massive, difficult knowledge constructions similar to HTML pages by lowering them to a canonical string model has no identify that I do know of. Martin Fowler urged “stringly asserted”, and from his suggestion comes the identify of this part.